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.ios

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.git

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.spa

Sample Input:

5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

Sample Output:

3
150702193604190912

題目限制:

image.png

題目大意:

給出全部校友會成員的id和全部參加慶祝客人的id,計算參加慶祝儀式的人中校友會的成員人數,並輸出年齡最大的那個校友會成員的id,若是沒有校友會成員參加,則輸出客人中年齡最大的人。code

算法思路:

首先使用unordered_map<string,bool> isAlumni存儲全部的校友,其次使用oldestBirth,oldestPeople分別表示賓客中最年長的生日和最年長的人id,oldestAlumni,oldestAlumniBirth分別表示校友中最年長的生日和最年長的人id,而後在輸入M個參加的賓客的時候,只要當前人更加年長就更新oldestBirth,oldestPeople,若是當前人仍是校友,更新oldestAlumni,oldestAlumniBirth,並使用alumniNum累計參加的校友人數,在輸入完畢以後,只要alumniNum不等於0,說明有校友參加,輸出alumniNum和oldestAlumni,不然輸出0和oldestPeople。orm

提交結果:

image.png

AC代碼:

#include<cstdio>
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

unordered_map<string,bool> 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<M;++i){
        string id;
        cin>>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;
}
相關文章
相關標籤/搜索