首页 > 其他 > 详细

PAT A1036 Boys vs Girls

时间:2019-08-23 16:20:49      阅读:103      评论:0      收藏:0      [点我收藏+]

PAT 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 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 /****************************************************
 2 PAT A1036 Boys vs Girls
 3 ****************************************************/
 4 #include <iostream>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 struct StuInfo {
10     string name = "";
11     char gender;
12     string id = "";
13     int grade = 0;
14 };
15 
16 int main() {
17     int stuCount = 0;
18     vector<StuInfo> maleStuList = { {"", M, "", 100 } };   //male[0]是male中grade最低的人的信息
19     vector<StuInfo> femaleStuList = { {"", F, "", 0 } };   //female[0]是female中grade最高的人的信息
20 
21     cin >> stuCount;
22 
23     StuInfo temp;
24     for (int i = 0; i < stuCount; ++i) {
25         cin >> temp.name >> temp.gender >> temp.id >> temp.grade;
26 
27         if (temp.grade < 0 || temp.grade > 100) continue;
28 
29         if (temp.gender == M) {
30             maleStuList.push_back(temp);
31 
32             if (temp.grade < maleStuList[0].grade) maleStuList[0] = temp;
33         }
34         else {
35             femaleStuList.push_back(temp);
36 
37             if (temp.grade > femaleStuList[0].grade) femaleStuList[0] = temp;
38         }
39     }
40 
41     femaleStuList.size() == 1 ? cout << "Absent" << endl : cout << femaleStuList[0].name <<   << femaleStuList[0].id << endl;
42     maleStuList.size() == 1 ? cout << "Absent" << endl : cout << maleStuList[0].name <<   << maleStuList[0].id << endl;
43     femaleStuList.size() == 1 || maleStuList.size() == 1 ? cout << "NA" : cout << femaleStuList[0].grade - maleStuList[0].grade;
44     
45     return 0;
46 }

 

注意事项:

  1:上述代码中用另个vector分别存储男生和女生的信息,用vector中第一个位置分别记录male中grade最小和female中grade最大的人员信息,vector首元素起到“哨兵”的作用。也可以使用两个int类型的值分别记录male中grade最小和female中grade最大的存储位置信息,这样可以避免结构体在赋值时的消耗,从而进一步提升算法速度,其具体实现不再赘述。

 

PAT A1036 Boys vs Girls

原文:https://www.cnblogs.com/mrdragon/p/11400698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!