题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016
题目描述
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 F −gradeM . If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.
样例输入
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
样例输出
Mary EE990830
Joe Math990112
6
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct student {
char name[12];
char gender;
char id[12];
char grade;
}f[1010], m[1010], temp;
bool cmp1 (student a, student b) {
return a.grade > b.grade;
}
bool cmp2 (student a, student b) {
return a.grade < b.grade;
}
int main() {
int n, num1 = 0, num2 = 0;
scanf("%d", &n);
while(n--) {
scanf("%s %c %s %d", temp.name, &temp.gender, temp.id, &temp.grade);
if(temp.gender == 'F')
f[num1++] = temp;
if(temp.gender == 'M')
m[num2++] = temp;
}
sort(f, f + num1, cmp1);
sort(m, m + num2, cmp2);
if(num1 > 0)
printf("%s %s\n", f[0].name, f[0].id);
else
printf("Absent\n");
if(num2 > 0)
printf("%s %s\n", m[0].name, m[0].id);
else
printf("Absent\n");
if(num1 == 0 || num2 ==0)
printf("NA\n");
else
printf("%d\n", f[0].grade - m[0].grade);
return 0;
}