19年春季第二題 PAT甲級 1157 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 NNN (≤105).
Then NNN 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 MMM(≤105). Then MMM 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
1
2
3
4
5
6
7
8
9
10
11
12
13

Sample Output:

3
150702193604190912

中文題目

7-5 校慶 (25 分)

2019 年浙江大學將要慶祝成立 122 週年。爲了準備校慶,校友會收集了所有校友的身份證號。現在需要請你編寫程序,根據來參加校慶的所有人士的身份證號,統計來了多少校友。

輸入格式:

輸入在第一行給出不超過 10^​5 的正整數 N,隨後 N 行,每行給出一位校友的身份證號(18 位由數字和大寫字母X組成的字符串)。題目保證身份證號不重複。

隨後給出前來參加校慶的所有人士的信息:首先是一個不超過 10​^5 的正整數 M,隨後 M 行,每行給出一位人士的身份證號。題目保證身份證號不重複。

輸出格式:

首先在第一行輸出參加校慶的校友的人數。然後在第二行輸出最年長的校友的身份證號 —— 注意身份證第 7-14 位給出的是 yyyymmdd 格式的生日。如果沒有校友來,則在第二行輸出最年長的來賓的身份證號。題目保證這樣的校友或來賓必是唯一的。

輸入樣例:

5

372928196906118710

610481197806202213

440684198612150417

13072819571002001X

150702193604190912

6

530125197901260019

150702193604190912

220221196701020034

610481197806202213

440684198612150417

370205198709275042

輸出樣例:

3

150702193604190912

 

分析

這道題沒用set或者unorder set存校友名錄會超時。

滿分代碼

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
using namespace std;
int main(){
	int n,m,cnt=0;
	cin>>n;
	getchar();
	string s,old="20190302",ans;
	set<string> st;
	for(int i=0;i<n;i++){
		getline(cin,s);
		st.insert(s);
	}
	cin>>m;
	getchar();
	for(int i=0;i<m;i++){
		getline(cin,s);
		set<string>::iterator it;
		if(st.find(s)!=st.end()){
            cnt++;
		}
		if(s.substr(6,8)<old){
				old=s.substr(6,8);
				ans=s;
		}
	}
	cout<<cnt<<endl;
	cout<<ans;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章