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
分别建立两个vector数组,然后分别进行两次排序,输出即可!
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;struct node{
string name;
char sex;
string ID;
int score;
};bool cmp1(node a, node b){
return a.score > b.score;
}
bool cmp2(node a, node b){
return a.score < b.score;
}
int main(){
int n, grade;
string s1, s2;
char c;
cin >> n;
vector<node> v1, v2;
node temp;
for(int i = 0; i < n; i++){
cin >> s1 >> c >> s2 >> grade;
temp = {s1, c, s2, grade};
if(c == 'M')
v1.push_back(temp);
else if (c == 'F')
v2.push_back(temp);
}
sort(v1.begin(), v1.end(), cmp2);
sort(v2.begin(), v2.end(), cmp1);
int cnt;
if(v1.size() != 0 && v2.size() != 0){
cnt = v2[0].score - v1[0].score;
cout << v2[0].name << " " << v2[0].ID << endl;
cout << v1[0].name << " " << v1[0].ID << endl;
cout << cnt;
}
else if(v1.size() == 0){
cout << v2[0].name << " " << v2[0].ID << endl;
cout << "Absent" << endl;
cout << "NA";
}
else if(v2.size() == 0){
cout << "Absent" << endl;
cout << v1[0].name << " " << v1[0].ID << endl;
cout << "NA";
}
return 0;
}
该博客要求计算所有男学生最低成绩与所有女学生最高成绩的差值。给出了输入输出规范和示例,还提供了实现代码。通过建立两个vector数组分别存储男女学生信息,进行两次排序后输出结果,若某类学生缺失则按特定格式输出。
406

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



