PAT Grade --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 namegenderID 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


 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <string>
 5 using namespace std;
 6 int N;
 7 struct Node
 8 {
 9     string name, gender, ID;
10     int grade;
11 }node;
12 int main()
13 {
14     cin >> N;
15     vector<Node>male, female;
16 
17     //This code is save the data in sorted, they obtain the highest and lowest points in the input, thus saving time and the space 
18 is      for ( int I = 0 ; I <N; ++ I)
 . 19      {
 20 is          node.name >> >> >> node.gender CIN node.ID >> node.grade;
 21 is          IF (node.gender == " M " )
 22 is              male.push_back (Node);
 23 is          the else 
24              female.push_back (Node );
 25      }
 26 is      Sort (male.begin (), male.end (), [] (the Node A, the Node B) { return a.grade < b.grade;});
 27     sort(female.begin(), female.end(), [](Node a, Node b) {return a.grade > b.grade; });
28     if (female.size() == 0)
29         cout << "Absent" << endl;
30     else
31         cout << female[0].name << " " << female[0].ID << endl;
32     if (male.size() == 0)
33         cout << "Absent" << endl;
35ELSE34     
         cout << male[0].name << " " << male[0].ID << endl;
36     if (female.size() == 0 || male.size() == 0)
37         cout << "NA" << endl;
38     else
39         cout << female[0].grade - male[0].grade << endl;
40     return 0;
41 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11256656.html