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 95Sample Output 1:
Mary EE990830 Joe Math990112 6Sample Input 2:
1 Jean M AA980920 60Sample Output 2:
Absent Jean AA980920NA
![]()
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstdio> using namespace std; struct Person{ string name , gender , id;//name, gender, ID and grade int grade; Person(){} Person(string _name , string _gender , string _id , int _grade):name(_name),gender(_gender),id(_id),grade(_grade){} }; bool cmp(const Person &a , const Person &b){return a.grade > b.grade;} int main() { int n=0; cin>>n; vector<Person> males , females; males.reserve(n); females.reserve(n); for(int i=0;i<n;i++){ Person p; cin>>p.name>>p.gender>>p.id>>p.grade; if(p.gender=="F")females.push_back(p); else males.push_back(p); } sort(females.begin() , females.end() , cmp); sort(males.begin() , males.end() , cmp); //gradeF-gradeM female highest male-loweset int gradeF=-1 , gradeM=-1; if(females.size()==0 )cout<<"Absent"<<endl; else { printf("%s %s\n",females.front().name.c_str() , females.front().id.c_str() ); gradeF=females.front().grade; } if(males.size()==0)cout<<"Absent"<<endl; else { printf("%s %s\n",males.back().name.c_str() , males.back().id.c_str() ); gradeM=males.back().grade; } if(gradeF==-1 || gradeM==-1)cout<<"NA"; else cout<<(gradeF-gradeM); return 0; }
性别成绩差异计算
本程序通过输入学生的姓名、性别、ID及成绩,计算并输出女生最高分与男生最低分的学生信息,同时给出两者之间的分数差距。若缺少某性别的学生,则相应输出'Absent'或'NA'。
1349

被折叠的 条评论
为什么被折叠?



