ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT甲级——A1036 Boys vs Girls

2019-07-27 21:02:31  阅读:256  来源: 互联网

标签:node PAT name grade Girls vs female line male


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 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 #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     //此代码是将数据保存下来排序,还可以直接在输入时就得到最高分和最低分,这样就大大节省了空间和时间
18     for (int i = 0; i < N; ++i)
19     {
20         cin >> node.name >> node.gender >> node.ID >> node.grade;
21         if (node.gender == "M")
22             male.push_back(node);
23         else
24             female.push_back(node);
25     }
26     sort(male.begin(), male.end(), [](Node a, 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;
34     else
35         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 }

 

标签:node,PAT,name,grade,Girls,vs,female,line,male
来源: https://www.cnblogs.com/zzw1024/p/11256656.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有