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
题意:
给定 N 个学生的成绩信息,请你求出女生第一名与男生倒数第一名的分数差距。
输入格式
第一行输入整数 N,表示学生数量
接下来 N 行,每行包含一个学生的姓名,性别,ID和成绩。其中姓名和ID是长度不超过 10 且不包含空格的字符串。性别为 F(女)或 M(男)。成绩是一个范围在 [0,100] 的整数。保证所有学生的成绩互不相同。
输出格式
输出共三行。
第一行输出女生第一名的姓名和ID。
第二行输出男生倒数第一名的姓名和ID。
第三行输出女生第一名的成绩与男生倒数第一名的成绩的差的绝对值。
如果不存在某个性别的学生,则在对应行输出 Absent。
在第三行输出 NA。
思路:
输入过程中,对男生、女生分开判断score找到女生最大id
、男生最小id
用map<stirng,pair<string,int>> mp
存储每个学生信息,根据前面获得的id可以找到对应的信息并输出
C++:
#include<bits/stdc++.h>
using namespace std;
map<string,pair<string,int>> mp;
int main(){
int n;
cin>>n;
int f_max=-1,m_min=110;
string f_max_id="",m_min_id="";
while(n--){
string s1,s2,s3;
int score;
cin>>s1>>s2>>s3>>score;
mp[s1] = {s3,score};
if(s2=="M"){
if(score<m_min) {
m_min = score;
m_min_id=s1;
}
}
else if(s2=="F"){
if(score>f_max) {
f_max = score;
f_max_id=s1;
}
}
}
int num = f_max - m_min;
if(f_max_id==""&&m_min_id!=""){
cout<<"Absent"<<endl<<m_min_id<<" "<<mp[m_min_id].first<<endl<<"NA";
}
else if(f_max_id!=""&&m_min_id==""){
cout<<f_max_id<<" "<<mp[f_max_id].first<<endl<<"Absent"<<endl<<"NA";
}
else{
cout<<f_max_id<<" "<<mp[f_max_id].first<<endl;
cout<<m_min_id<<" "<<mp[m_min_id].first<<endl<<num;
}
return 0;
}