1036 Boys vs Girls
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 grade
g
r
a
d
e
grade
gradeF−
g
r
a
d
e
grade
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
Analysis
给出 n 个同学信息,先输出女生中最高分对应的学生姓名和id,后输出男生中最低分对应学生姓名和id,最后一行输出女生最高分 减 男生最低分(前两行输出的两个人的分差)。
注意:如果不存在女生 或 男生,则其对应输出为Absent
,最后一行的分差则输出NA
Code
结构体分别存储男生信息male[ ]
和女生信息female[ ]
;
变量mcnt
和fcnt
分别表示输入的男生和女生的人数;
分别对男生和女生结构体按成绩进行升序和降序排序,最后按要求输出即可。
#include<bits/stdc++.h>
using namespace std;
struct person
{
string name,id;
int score;
}male[110],female[110];
bool mcmp(person x,person y)
{
return x.score<y.score;
}
bool fcmp(person x,person y)
{
return x.score>y.score;
}
int main()
{
int n;
cin>>n;
char gender;
string name,id;
int score,mcnt=0,fcnt=0;
for(int i=0,j=0;(i+j)<=n;)
{
cin>>name>>gender>>id>>score;
if(gender=='M')
{
male[i].name=name;
male[i].id=id;
male[i++].score=score;
mcnt++;
}
else
{
female[j].name=name;
female[j].id=id;
female[j++].score=score;
fcnt++;
}
}
sort(male,male+mcnt,mcmp);
sort(female,female+fcnt,fcmp);
if(!fcnt)
cout<<"Absent"<<endl;
else
cout<<female[0].name<<" "<<female[0].id<<endl;
if(!mcnt)
cout<<"Absent"<<endl;
else
cout<<male[0].name<<" "<<male[0].id<<endl;
if(fcnt && mcnt)
cout<<female[0].score-male[0].score<<endl;
if(!fcnt || !mcnt)
cout<<"NA"<<endl;
return 0;
}