测试地址:☞
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
题意:
给出学生的姓名、性别、学号和成绩,输出 女生中最高成绩学生 和 男生中最低成绩学生 的姓名和学号,以及他们的成绩差;如果不存在女生或男生,对应位置输出 Absent,差值处输出 NA。
思路:
用 string 类型保存学生的信息,F_Grade 和 M_Grade 分别保存女生的最高分和男生的最低分,可以用一个 bool 型变量来判断男女生是否存在,也可以看最后的分数是否和初值相等来判断。
c++代码1:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, F_Grade=0, M_Grade=120;
cin >> n;
string F_Name, F_ID, M_Name, M_ID;
bool flag1, flag2;
flag1 = flag2 = false;
for(int i = 0; i < n; i++){
int grade;
string name, gender, id;
cin >> name >> gender >> id >> grade;
if(gender == "F"){
flag1 = true;
if(F_Grade < grade){
F_Grade = grade;
F_Name = name;
F_ID = id;
}
}
else{
flag2 = true;
if(M_Grade > grade){
M_Grade = grade;
M_Name = name;
M_ID = id;
}
}
}
if(flag1) cout << F_Name << " " << F_ID << endl;
else cout << "Absent" << endl;
if(flag2) cout << M_Name << " " << M_ID << endl;
else cout << "Absent" << endl;
if(flag1 && flag2) cout << abs(F_Grade - M_Grade);
else cout << "NA";
return 0;
}
c++代码2:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, F_Grade=-1, M_Grade=120;
cin >> n;
string F, M;
for(int i = 0; i < n; i++){
int grade;
string name, gender, id;
cin >> name >> gender >> id >> grade;
if(gender == "F"){
if(F_Grade < grade){
F_Grade = grade;
F = name + " " + id;
}
}
else{
if(M_Grade > grade){
M_Grade = grade;
M = name + " " + id;
}
}
}
if(F_Grade != -1) cout << F << endl;
else cout << "Absent" << endl;
if(M_Grade != 120) cout << M << endl;
else cout << "Absent" << endl;
if(F_Grade!=-1 && M_Grade!=120) cout << abs(F_Grade - M_Grade);
else cout << "NA";
return 0;
}