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). 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). 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
题目大意:为了准备校庆,校友会收集了所有校友的身份证号。编写程序,根据来参加校庆的所有人士的身份证号,统计来了多少校友。输入在第一行正整数N,随后N行,每行给出一位校友的身份证号(18位由数字和大写字母X组成的字符串)。题目保证身份证号不重复。随后给出前来参加校庆的所有人士的信息:首先是一个不超过正整数M,随后M行,每行给出一位人士的身份证号。题目保证身份证号不重复。首先在第一行输出参加校庆的校友的人数。然后在第二行输出最年长的校友的身份证号——注意身份证第7-14位给出的是yyyymmdd格式的生日。如果没有校友来,则在第二行输出最年长的来宾的身份证号。题目保证这样的校友或来宾必是唯一的。
分析:使用容器set<string> record存储校友的身份证信息,smallx记录校友中最年长的生日,smalla记录所有人中最年长的生日,ansx记录最年长的校友的身份证号,ansa记录所有人中最年长的人的身份证号,cnt记录有多少校友参加。将校友身份证存储在set容器record中,使用record.count来判断参加校庆的人是否是校友,使用substr(6, 8)快速提取身份证号中的生日信息,最后根据cnt的值输出~cnt不等于0,则说明有校友参加,输出最年长的校友的身份证号ansx;如果cnt等于0,说明没有校友参加,输出所有人中最年长的人的身份证号ansa~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <set> using namespace std; int n, m, cnt; string temp, smallx = "99999999", smalla = "99999999", ansx, ansa; set<string> record; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> temp; record.insert(temp); } cin >> m; for (int i = 0; i < m; i++) { cin >> temp; if (record.count(temp)) { cnt++; if (smallx > temp.substr(6, 8)) { smallx = temp.substr(6, 8); ansx = temp; } } if (smalla > temp.substr(6, 8)) { smalla = temp.substr(6, 8); ansa = temp; } } cout << cnt << endl; if (cnt) cout << ansx; else cout << ansa; return 0; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼