2019 年浙江大学将要庆祝成立 122 周年。为了准备校庆,校友会收集了所有校友的身份证号。现在需要请你编写程序,根据来参加校庆的所有人士的身份证号,统计来了多少校友。
输入格式:
输入在第一行给出不超过 105 的正整数 N,随后 N 行,每行给出一位校友的身份证号(18 位由数字和大写字母X组成的字符串)。题目保证身份证号不重复。
随后给出前来参加校庆的所有人士的信息:首先是一个不超过 105 的正整数 M,随后 M 行,每行给出一位人士的身份证号。题目保证身份证号不重复。
输出格式:
首先在第一行输出参加校庆的校友的人数。然后在第二行输出最年长的校友的身份证号 —— 注意身份证第 7-14 位给出的是 yyyymmdd
格式的生日。如果没有校友来,则在第二行输出最年长的来宾的身份证号。题目保证这样的校友或来宾必是唯一的。
输入样例:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
输出样例:
3
150702193604190912
C:
#include <stdio.h>
#include <string.h>
#include <set>
using namespace std;
int n, m;
set<string> S, S2;
char temp[20], smallx[20] = "99999999", smalla[20] = "99999999", ansx[20], ansa[20];
int main()
{
int cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%s", temp);
S.insert(temp);
}
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%s", temp);
S2.insert(temp);
if (S.count(temp))
{
cnt++;
if (strcmp(smallx, temp + 6) > 0)
{
strcpy(smallx, temp + 6);
strcpy(ansx, temp);
}
}
if (strcmp(smalla, temp + 6) > 0)
{
strcpy(smalla, temp + 6);
strcpy(ansa, temp);
}
}
printf("%d\n", cnt);
if (cnt)
{
printf("%s", ansx);
}
else
{
printf("%s", ansa);
}
return 0;
}
C++:
#include <bits/stdc++.h>
using namespace std;
int n,m;
set<string> S,S2;
string temp,smallx="99999999",smalla="99999999",ansx,ansa;
int main()
{
int cnt=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>temp;
S.insert(temp);
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
cin>>temp;
S2.insert(temp);
if(S.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;
}
Java:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Set<String> S = new HashSet<>();
for (int i = 0; i < n; i++) {
String temp = scanner.next();
S.add(temp);
}
int m = scanner.nextInt();
Set<String> S2 = new HashSet<>();
String smallx = "99999999";
String smalla = "99999999";
String ansx = "";
String ansa = "";
for (int i = 0; i < m; i++) {
String temp = scanner.next();
S2.add(temp);
if (S.contains(temp)) {
if (smallx.compareTo(temp.substring(6, 8)) > 0) {
smallx = temp.substring(6, 8);
ansx = temp;
}
}
if (smalla.compareTo(temp.substring(6, 8)) > 0) {
smalla = temp.substring(6, 8);
ansa = temp;
}
}
int cnt = 0;
for (String temp : S2) {
if (S.contains(temp)) {
cnt++;
}
}
System.out.println(cnt);
if (cnt > 0) {
System.out.println(ansx);
} else {
System.out.println(ansa);
}
}
}
Python:
n = int(input())
S = set()
for _ in range(n):
temp = input()
S.add(temp)
m = int(input())
S2 = set()
smallx = "99999999"
smalla = "99999999"
ansx = ""
ansa = ""
for _ in range(m):
temp = input()
S2.add(temp)
if temp in S:
if smallx > temp[6:8]:
smallx = temp[6:8]
ansx = temp
if smalla > temp[6:8]:
smalla = temp[6:8]
ansa = temp
cnt = len(S2.intersection(S))
print(cnt)
if cnt > 0:
print(ansx)
else:
print(ansa)
总结:
首先,定义了一些变量,包括整数变量 n 和 m,两个集合 S 和 S2,以及一些字符串变量。
通过 scanf 从输入中读取一个整数 n,表示要读取的字符串数量。然后,使用一个循环读取 n 个字符串,并将它们插入到集合 S 中。
接下来,通过 scanf 再次从输入中读取一个整数 m,表示要读取的另一个字符串数量。然后,使用一个循环读取 m 个字符串,并将它们插入到集合 S2 中。
在读取过程中,首先检查当前读取的字符串是否存在于集合 S 中。如果存在,将 cnt 变量加一,并检查它的子字符串的某个部分是否比之前的最小值要小。如果是,则更新最小值和相应的字符串。
在循环结束后,输出 cnt 的值,表示存在于集合 S 和集合 S2 中共有多少个相同的字符串。
根据 cnt 的值,决定输出结果。如果 cnt 大于 0,则输出之前找到的具有最小子字符串值的字符串 ansx;否则,输出具有最小子字符串值的字符串 ansa。