问题描述
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.
输入格式
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.
输出格式
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 grade gradeF - gradeM . If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.
输入样例1
3 Joe M Math990112 89 Mike M CS991301 100 Mary F EE990830 95 (结尾无空行)
输出样例1
Mary EE990830 Joe Math990112 6 (结尾无空行)
输入样例2
1 Jean M AA980920 60 (结尾无空行)
输出样例2
Absent Jean AA980920 NA (结尾无空行)
C语言代码
#include<stdio.h>
#include<string.h>
const int MAX = 15;
typedef struct{
char name[MAX];
char gender;
char ID[MAX];
int grade;
}student;
void init(student* male_s, student* female_s){
male_s->name [0]= female_s->name[0] = '\0';
male_s->gender = 'M';
female_s->gender = 'F';
male_s->ID[0] = female_s->ID[0] = '\0';
male_s->grade = 101;
female_s->grade = -1;
}
int main(){
int n, grade;
char name[MAX], gender, ID[MAX];
student female , male;
init(&male , &female);
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s %c %s %d", name, &gender, ID, &grade);
if(gender == 'M' && grade < male.grade){
strcpy(male.name , name);
strcpy(male.ID , ID);
male.grade = grade;
}
if(gender == 'F' && grade > female.grade){
strcpy(female.name , name);
strcpy(female.ID , ID);
female.grade = grade;
}
}
if(female.grade == -1)
printf("Absent\n");
else
printf("%s %s\n", female.name, female.ID);
if(male.grade == 101)
printf("Absent\n");
else
printf("%s %s\n", male.name, male.ID);
if(female.grade != -1 && male.grade != 101)
printf("%d", female.grade-male.grade);
else
printf("NA");
return 0;
}
注意:
- 初始化结构体时,函数不能直接传入结构体,应该传入结构体的地址,才能返回得到初始化的结构,否则初始化是无效的
- 初始化时,男生、女生的成绩不能简单的初始化为0或者100,会影响后续对是否有此类学生的判断,根据学生成绩在 [0,100] 将其初始化为该范围外的值
Boys VS Girls 原题
|