1036 Boys vs Girls分数 25
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.
每个输入文件包含一个测试用例。每个案例都包含一个正整数N,后面跟着N行学生信息。每行包含一个学生的姓名、性别、身份证和成绩,用空格分隔,其中姓名和身份证是不超过10个字符的字符串,没有空格,性别是F(女性)或M(男性),成绩是0到100之间的整数。保证所有分数都是不同的。
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.
对于每个测试用例,在3行中输出。第一行给出成绩最高的女生的姓名和身份证,第二行给出成绩最低的男生的姓名和证件。第三行给出了差值等级F−等级M。如果缺少一个这样的学生,则在相应的行中输出Absent,而在第三行中输出NA。
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
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
struct student{
string name,id;
int grade;
}high_female,low_male,cur;
int main(){
int n,i;
int cha;
cin>>n;
high_female.grade=-1,low_male.grade=101;
char gender;
for(i=0;i<n;i++){
cin>>cur.name>>gender>>cur.id>>cur.grade;
if(gender=='M'){//男生,找最低成绩
if(cur.grade<low_male.grade)low_male=cur;
}
if(gender=='F'){//女生,找最高成绩
if(cur.grade>high_female.grade)high_female=cur;
}
cha=abs(low_male.grade-high_female.grade);
}
if(high_female.grade==-1){//女生最高成绩还是为初始值-1,没有改变,说明没有女生
cout<<"Absent"<<endl;
}
else{
cout<<high_female.name<<" "<<high_female.id<<endl;
}
if(low_male.grade==101){//男生最低成绩还是为初始值101,没有改变,说明没有男生
cout<<"Absent"<<endl;
}
else{
cout<<low_male.name<<" "<<low_male.id<<endl;
}
if(high_female.grade==-1 || low_male.grade==101){
cout<<"NA"<<endl;
}
else{
cout<<cha<<endl;
}
}