PAT甲级 1036. Boys vs Girls (25)

来源:互联网 发布:钢管舞教学视频软件 编辑:程序博客网 时间:2024/05/22 10:39

题目:

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:

3Joe M Math990112 89Mike M CS991301 100Mary F EE990830 95
Sample Output 1:
Mary EE990830Joe Math9901126
Sample Input 2:
1Jean M AA980920 60
Sample Output 2:
AbsentJean AA980920NA
思路:

这个可用vector的排序,先排性别再排分数,这样排序之后,一般序列第一个是女最高分,最后一个是男最低分。

需注意只有一个学生和没有学生的情况。这次的代码因为逻辑简单,没有在排序之后,再进行进一步处理,就直接分情况输出了,代码写得有点繁琐。


代码:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;struct student{string name;char gender;string ID;int grade;bool operator<(const student &s1)const{if (this->gender != s1.gender){return this->gender < s1.gender;}else{return this->grade > s1.grade;}}};int main(){//inputint N;cin >> N;int i = 0;vector<student> stu(N);for (; i < N; ++i){cin >> stu[i].name >> stu[i].gender >> stu[i].ID >> stu[i].grade;}sort(stu.begin(),stu.end());if (N >=2){if (stu[0].gender == 'F'){cout << stu[0].name << " " << stu[0].ID << endl;i = stu.size() - 1;if (stu[i].gender == 'M'){cout<< stu[i].name << " " << stu[i].ID << endl;cout << stu[0].grade - stu[i].grade << endl;}else{cout << "Absent" << endl;cout << "NA"<< endl;}}else{cout << "Absent" << endl;if (stu[i].gender == 'M'){cout << stu[i].name << " " << stu[i].ID << endl;}cout << "NA" << endl;}}else{if (N == 0){cout<< "Absent" << endl;cout << "Absent" << endl;cout << "NA" << endl;}if (N == 1){if (stu[0].gender == 'F'){cout << stu[0].name << " " << stu[0].ID << endl;cout<< "Absent" << endl;}else{cout << "Absent" << endl;cout << stu[0].name << " " << stu[0].ID << endl;}cout << "NA" << endl;}}system("pause");return 0;}


原创粉丝点击