• 1157 Anniversary – PAT甲级真题


    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 record存储校友的身份证信息,smallx记录校友中最年长的生日,smalla记录所有人中最年长的生日,ansx记录最年长的校友的身份证号,ansa记录所有人中最年长的人的身份证号,cnt记录有多少校友参加。将校友身份证存储在set容器record中,使用record.count来判断参加校庆的人是否是校友,使用substr(6, 8)快速提取身份证号中的生日信息,最后根据cnt的值输出~cnt不等于0,则说明有校友参加,输出最年长的校友的身份证号ansx;如果cnt等于0,说明没有校友参加,输出所有人中最年长的人的身份证号ansa~

    1. #include
    2. #include
    3. using namespace std;
    4. int n, m, cnt;
    5. string temp, smallx = "99999999", smalla = "99999999", ansx, ansa;
    6. set record;
    7. int main() {
    8. cin >> n;
    9. for (int i = 0; i < n; i++) {
    10. cin >> temp;
    11. record.insert(temp);
    12. }
    13. cin >> m;
    14. for (int i = 0; i < m; i++) {
    15. cin >> temp;
    16. if (record.count(temp)) {
    17. cnt++;
    18. if (smallx > temp.substr(6, 8)) {
    19. smallx = temp.substr(6, 8);
    20. ansx = temp;
    21. }
    22. }
    23. if (smalla > temp.substr(6, 8)) {
    24. smalla = temp.substr(6, 8);
    25. ansa = temp;
    26. }
    27. }
    28. cout << cnt << endl;
    29. if (cnt) cout << ansx;
    30. else cout << ansa;
    31. return 0;
    32. }

  • 相关阅读:
    35岁失业程序员的在线简历制作工具
    包含光栅的高NA显微系统
    一文看懂分布式存储架构
    web网页设计期末课程大作业——电影介绍5页HTML+CSS制作
    一款mysql审计日志自动轮转自动清理工具:mysql-audit-extend
    flutter跨端开发for Web、Windows Q&A (持续补充中)
    Linux学习之vim跳转到特定行数
    java基于mvc的高校教师科研信息管理系统
    Scala 基础 (四):函数式编程【从基础到高阶应用】
    秀动脚本增加微信通知和多账号抢购
  • 原文地址:https://blog.csdn.net/liuchuo/article/details/126222611