A1036 Boys vs Girls
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
思路:
开两个结构体动态数组,boy(n)和girl(n),然后分别排序输出最小和最大;输出时考虑Absent和NA情况。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 struct Students 7 { 8 string name; 9 char gender; 10 string id; 11 int grade; 12 }; 13 bool cp(Students a, Students b) { 14 return a.grade < b.grade; 15 } 16 int main() { 17 int n; 18 cin >> n; 19 string namei, idi; 20 char genderi; 21 int gradei; 22 vector<Students>boy(n); 23 vector<Students>girl(n); 24 int j = 0, k = 0; 25 for (int i = 0; i < n; i++) { 26 cin >> namei >> genderi >> idi >> gradei; 27 if (genderi == 'M') { 28 boy[j].name = namei; 29 boy[j].gender = genderi; 30 boy[j].id = idi; 31 boy[j].grade = gradei; 32 j++; 33 } 34 else { 35 girl[k].name = namei; 36 girl[k].gender = genderi; 37 girl[k].id = idi; 38 girl[k].grade = gradei; 39 k++; 40 } 41 } 42 sort(boy.begin(), boy.begin()+j, cp); 43 sort(girl.begin(), girl.begin()+k, cp); 44 if (k == 0)cout << "Absent" << endl; 45 else cout << girl[k - 1].name << " " << girl[k - 1].id<<endl; 46 if (j == 0)cout << "Absent" << endl; 47 else cout << boy[0].name << " " << boy[0].id << endl; 48 if (j == 0 || k == 0)cout << "NA"; 49 else cout << girl[k - 1].grade - boy[0].grade; 50 return 0; 51 }