PAT(甲级)2019年春季考试 7-2 Anniversary

7-2 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 (≤10​^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(≤10^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(甲级)2019年春季考试 7-2 Anniversary_第1张图片

题目大意:

给出所有校友会成员的id和所有参加庆祝客人的id,计算参加庆祝仪式的人中校友会的成员人数,并输出年龄最大的那个校友会成员的id,如果没有校友会成员参加,则输出客人中年龄最大的人。

算法思路:

首先使用unordered_map isAlumni存储所有的校友,其次使用oldestBirth,oldestPeople分别表示宾客中最年长的生日和最年长的人id,oldestAlumni,oldestAlumniBirth分别表示校友中最年长的生日和最年长的人id,然后在输入M个参加的宾客的时候,只要当前人更加年长就更新oldestBirth,oldestPeople,如果当前人还是校友,更新oldestAlumni,oldestAlumniBirth,并使用alumniNum累计参加的校友人数,在输入完毕之后,只要alumniNum不等于0,说明有校友参加,输出alumniNum和oldestAlumni,否则输出0和oldestPeople。

提交结果:

PAT(甲级)2019年春季考试 7-2 Anniversary_第2张图片

AC代码:

#include
#include 
#include 
#include 
#include 

using namespace std;

unordered_map isAlumni;// 统计参加的人是否是校友

int main(){
    int N;
    scanf("%d",&N);
    for (int i = 0; i < N; ++i) {
        string id;
        cin>>id;
        isAlumni[id] = true;
    }
    int M;
    scanf("%d",&M);
    // M个参会的人数
    string oldestBirth;
    string oldestPeople;
    string oldestAlumni;
    string oldestAlumniBirth;
    int alumniNum = 0;
    for(int i=0;i>id;
        if(i==0){
            oldestPeople = id;
            oldestBirth = id.substr(6,8);
        } else if(oldestBirth>id.substr(6,8)) {
            oldestPeople = id;
            oldestBirth = id.substr(6,8);
        }
        if(isAlumni[id]){
            ++alumniNum;
            // 当前是校友
            if(alumniNum==1){
                oldestAlumni = id;
                oldestAlumniBirth = id.substr(6,8);
            }else if(oldestAlumniBirth>id.substr(6,8)) {
                oldestAlumni = id;
                oldestAlumniBirth = id.substr(6,8);
            }
        }
    }
    if(alumniNum!=0){
        printf("%d\n%s",alumniNum,oldestAlumni.c_str());
    } else {
        printf("0\n%s",oldestPeople.c_str());
    }
    return 0;
}

你可能感兴趣的:(算法-数据结构,c++)