PAT甲级【2019年3月考题】——A1157 Anniversary【25】

Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID’s of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤105 10^510
5
​​ ). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID’s are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤105 10^510
5
​​ ).Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID’s are distinct.

Output Specification:
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus – notice that the 7th - 14th digits of the ID gives one’s birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
Sample Output:
3
150702193604190912

【声明】

  由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

Solution:

  这道题就是说,浙大举行校区,列了n个贵宾id【身份证】,然后问出席的m个人中有没有贵宾出席,有的话,输出出席的最大年龄贵宾的id,若没有贵宾出席,那么就输出出席人中的最大年龄的id

  这道题就使用unordered_map存储一下贵宾id,然后与出席的人的id对比一下即可,同时找到最大年龄的id

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <unordered_map>
 5 using namespace std;
 6 int main()
 7 {
 8     int n, m, cnt = 0;
 9     cin >> n;
10     unordered_map<string, bool>map;
11     string id, oldGuset = "20201231", oldAlumni = "20201231", gusetId, alumniId;//这里最老的人物的年龄使用的是明年的应该可以的
12     for (int i = 0; i < n; ++i)
13     {
14         cin >> id;
15         map[id] = true;//记录通知的人
16     }
17     cin >> m;
18     while (m--)
19     {
20         cin >> id;
21         string str = id.substr(6, 8);//获取出生年月
22         if (oldGuset > str)//年龄越大,出生时间越早
23         {
24             oldGuset = str;
25             gusetId = id;
26         }
27         if (map[id])//此人是贵宾
28         {
29             ++cnt;
30             if (oldAlumni > str)
31             {
32                 oldAlumni = str;
33                 alumniId = id;
34             }
35         }
36     }
37     printf("%d\n", cnt);
38     if (cnt > 0)
39         printf("%s\n", alumniId.c_str());
40     else
41         printf("%s\n", gusetId);    
42     return 0;
43 }

猜你喜欢

转载自www.cnblogs.com/zzw1024/p/11954202.html