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 95Sample Output 1:
Mary EE990830 Joe Math990112 6Sample Input 2:
1 Jean M AA980920 60Sample Output 2:
Absent Jean AA980920 NA
题目大意:
输入格式为N(跟N行数据),每行数据包括学生name(姓名)、gender(性别)、id(学号)、grade(成绩);
输出成绩最高的女生和成绩最低的男生,最后输出成绩差;如果不存在男生或女生输出Absent;若输出Absent则成绩差为NA;
思路:
定义结构体用于存储四种数据,temp读入数据,male储存成绩最低的男生,female储存成绩最高的女生;
代码如下:
#include <cstdio>
struct person {
char name [15];
char id[15];
int grade;
}temp, male, female;
void init() {
male.grade = 101; //超出grade的取值范围,则只要读入数据grade值一定会改变;
female.grade = -1;
}
int main()
{
init();
int m;
char gender;
scanf ("%d", &m);
for (int i = 0; i < m; i++) {
scanf ("%s %c %s %d", temp.name, &gender, temp.id, &temp.grade); //注意取地址符,仅字符串scanf时不用加& ;
if (gender == 'M' && temp.grade < male.grade) male = temp;
else if (gender == 'F' && temp.grade > female.grade) female = temp;
}
if (female.grade == -1) printf ("Absent\n"); //如若grade的初始值为变动,则认为不存在这种类型的学生;
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 ("NA");
else printf ("%d", female.grade - male.grade);
return 0;
}