1036 Boys vs Girls (男孩vs女孩)(25分)——C语言实现

1036 Boys vs Girls (男孩vs女孩)(25分)——C语言实现1036 Boys vs Girls 男孩 vs 女孩 This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students

大家好,我是讯享网,很高兴认识大家。

1036 Boys vs Girls (男孩vs女孩)

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM. If one such kind of student is missing, output “Absent” in the corresponding line, and output “NA” in the third line instead.


讯享网

Sample Input 1:

3 Joe M Math 89 Mike M CS 100 Mary F EE 95 

讯享网

Sample Output 1:

讯享网Mary EE Joe Math 6 

Sample Input 2:

1 Jean M AA 60 

Sample Output 2:

讯享网Absent Jean AA NA 

题目大意:

给出N个同学的信息,输出女生中的最高分获得者的信息与男生中最低分获得者的信息,并输出他们的分数差Girl.grade - Boy.grade。如果不存在女生或者男生,则对应获得者信息处输出Absent,而且差值处输出NA。

思路:

Boy、Girl两个结构体分别存储男生中的最低分的信息与女生中的最高分的信息;每次输入时对比,若是男生且text.grade < Boy.grade,则更新Boy结构体信息,若是女生且text.grade > Girl.grade,则更新Girl结构体的信息。

代码:

#include<stdio.h> #include<string.h> typedef struct node{ 
    char name[10]; char gender; char ID[10]; int grade; }Node; int main(){ 
    int n, i, flag = 1; scanf("%d", &n); Node text, Boy, Girl; Boy.grade = 101; Girl.grade = -1; for(i = 0; i < n; i++){ 
    getchar(); scanf("%s %s %s %d", text.name, &text.gender, text.ID, &text.grade); if(text.gender == 'F' && text.grade > Girl.grade){ 
    strcpy(Girl.name, text.name); strcpy(Girl.ID, text.ID); Girl.grade = text.grade; } if(text.gender == 'M' && text.grade < Boy.grade){ 
    strcpy(Boy.name, text.name); strcpy(Boy.ID, text.ID); Boy.grade = text.grade; } } if(Girl.grade == -1){ 
    printf("Absent\n"); flag = 0; } else{ 
    printf("%s %s\n", Girl.name, Girl.ID); } if(Boy.grade == 101){ 
    printf("Absent\n"); flag = 0; } else{ 
    printf("%s %s\n", Boy.name, Boy.ID); } if(flag == 0) printf("NA\n"); else printf("%d\n", Girl.grade - Boy.grade); return 0; } 
小讯
上一篇 2025-02-21 11:46
下一篇 2025-03-04 18:39

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/23121.html