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 Math990112 89 Mike M CS991301 100 Mary F EE990830 95
Sample Output 1:
Mary EE990830 Joe Math990112 6
Sample Input 2:
1 Jean M AA980920 60
Sample Output 2:
Absent Jean AA980920 NA
1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 typedef struct{ 6 char name[11]; 7 char id[11]; 8 char gender; 9 int grade; 10 }info; 11 int main(){ 12 int N, absent = 0; 13 info boy, girl, temp; 14 boy.grade = 101; 15 girl.grade = -1; 16 scanf("%d", &N); 17 for(int i = 0; i < N; i++){ 18 scanf("%s %c %s %d", temp.name, &(temp.gender), temp.id, &(temp.grade)); 19 if(temp.gender == 'F'){ 20 if(temp.grade > girl.grade){ 21 girl = temp; 22 strcpy(girl.name, temp.name); 23 strcpy(girl.id, temp.id); 24 } 25 }else if(temp.gender == 'M'){ 26 if(temp.grade < boy.grade){ 27 boy = temp; 28 strcpy(boy.name, temp.name); 29 strcpy(boy.id, temp.id); 30 } 31 } 32 } 33 if(girl.grade == -1 || boy.grade == 101) 34 absent = 1; 35 if(girl.grade == -1) 36 printf("Absent "); 37 else 38 printf("%s %s ", girl.name, girl.id); 39 if(boy.grade == 101) 40 printf("Absent "); 41 else 42 printf("%s %s ", boy.name, boy.id); 43 if(absent == 1) 44 printf("NA "); 45 else 46 printf("%d", girl.grade - boy.grade); 47 cin >> N; 48 return 0; 49 }
总结:
1、与1006一样,信息排序模拟,使用struct 结构体记录信息项。尽量边读入边处理,不需要全部存储后再排序。