1036 Boys vs Girls (25)
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~F~-grade~M~. 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
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 struct node{ 6 char name[11], id[11]; 7 int score; 8 }; 9 bool cmp(node a, node b){return a.score<b.score;} 10 int main(){ 11 int n, i; 12 cin>>n; 13 vector<node> female, male; 14 for(i=0; i<n; i++){ 15 node stu; 16 char sex; 17 scanf("%s %c %s %d", stu.name, &sex, stu.id, &stu.score); 18 if(sex=='M') male.push_back(stu); 19 else female.push_back(stu); 20 } 21 sort(male.begin(), male.end(), cmp); 22 sort(female.begin(), female.end(), cmp); 23 bool flag = true; 24 if(female.size()==0) {cout<<"Absent\n"; flag=false;} 25 else printf("%s %s\n", female[female.size()-1].name, female[female.size()-1].id); 26 if(male.size()==0) {cout<<"Absent\n"; flag=false;} 27 else printf("%s %s\n", male[0].name, male[0].id); 28 if(flag) cout<<female[female.size()-1].score-male[0].score<<endl; 29 else cout<<"NA"<<endl; 30 return 0; 31 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步