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
思路
这题也很简单,这里提醒一下自己,能用string就用string,用char数组真的太麻烦了……赋值还得用strcpy()。
然后变量写的尽量清楚一点,我就是gender和grade这两个单词太像了,写第二个比较函数的时候直接用了Code::Blocks自动填充出来的东西,结果一直有一个测试点错误,检查了一下才发现居然第二个比较函数比较的是gender(惊了,这样也能过三个测试点……)。
题目本身很简单,就是一个排序题,把男生女生分开来排序,女生按成绩从高到低排,男生按成绩从低到高排,另外,不存在的情况只要通过男生人数和女生人数来判断就好了,如果男生人数或者女生人数为0,就输出Absent。最后一行的话,需要男生人数和女生人数都不为0,才会有结果,否则就输出NA。
代码
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1010;
struct student{
string name;
char gender;
string ID;
int grade;
}stu[maxn], stuM[maxn], stuF[maxn];
bool cmpF(student a, student b){
return a.grade>b.grade;
}
bool cmpM(student a, student b){
return a.grade<b.grade;
}
int main(){
int N;
cin>>N;
int MaleNumber = 0, FemaleNumber = 0;
for(int i=0;i<N;i++){
cin>>stu[i].name>>stu[i].gender>>stu[i].ID>>stu[i].grade;
if(stu[i].gender=='M'){
stuM[MaleNumber].name = stu[i].name;
stuM[MaleNumber].gender = stu[i].gender;
stuM[MaleNumber].ID = stu[i].ID;
stuM[MaleNumber].grade = stu[i].grade;
MaleNumber++;
}
else{
stuF[FemaleNumber].name = stu[i].name;
stuF[FemaleNumber].gender = stu[i].gender;
stuF[FemaleNumber].ID = stu[i].ID;
stuF[FemaleNumber].grade = stu[i].grade;
FemaleNumber++;
}
}
if(FemaleNumber!=0){
sort(stuF, stuF+FemaleNumber, cmpF);
cout<<stuF[0].name<<" "<<stuF[0].ID<<'\n';
}
else printf("Absent\n");
if(MaleNumber!=0){
sort(stuM, stuM+MaleNumber, cmpM);
cout<<stuM[0].name<<" "<<stuM[0].ID<<'\n';
}
else printf("Absent\n");
if(FemaleNumber!=0&&MaleNumber!=0){
cout<<stuF[0].grade-stuM[0].grade;
}
else printf("NA");
return 0;
}