题目
2019年浙江大学将要庆祝成立 122 周年。
为了准备校庆,校友会收集了所有校友的身份证号。
现在需要请你编写程序,根据来参加校庆的所有人士的身份证号,统计来了多少校友。
输入格式
输入在第一行给出正整数 N。
随后 N 行,每行给出一位校友的身份证号(18 位由数字和大写字母 X 组成的字符串)。题目保证身份证号不重复。
随后给出前来参加校庆的所有人士的信息:
首先是一个正整数 M。
随后 M 行,每行给出一位人士的身份证号。题目保证身份证号不重复。
输出格式
首先在第一行输出参加校庆的校友的人数。
然后在第二行输出最年长的校友的身份证号 —— 注意身份证第 7−14位给出的是 yyyymmdd 格式的生日。
如果没有校友来,则在第二行输出最年长的来宾的身份证号。题目保证这样的校友或来宾必是唯一的。
数据范围
1≤N,M≤105
输入样例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 5 372928196906118710 610481197806202213 440684198612150417 13072819571002001X 150702193604190912 6 530125197901260019 150702193604190912 220221196701020034 610481197806202213 440684198612150417 370205198709275042
|
输出样例:
笔者解析
本题的输入输量巨大,而输出量却相对较少。在java编程中对于巨大的输出,我们应该尽量选择使用BufferedReader,而不是使用Scanner进行输入。两者的输入输出效率差异巨大,具体可由下图所示。
Scanner输入

BufferedReader输入

可见两者的效率差距还是特别巨大的,所以在这种输入量比较大的情况下尽量使用BufferedReader进行输入
笔者代码
Scanner版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import java.util.HashSet; import java.util.Scanner;
public class Main { public static void main(String[] args) { HashSet<String> alumnus = new HashSet<>(); HashSet<String> come = new HashSet<>(); HashSet<String> alumnusCome = new HashSet<>();
Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { alumnus.add(scanner.next()); }
int m = scanner.nextInt(),count = 0; String temp; for (int i = 0; i < m; i++) { temp = scanner.next();come.add(temp); if(alumnus.contains(temp)){ alumnusCome.add(temp); count++; } }
System.out.println(count); int old = Integer.MAX_VALUE,t; String oldNum = null; if(count>0){ come = alumnusCome; } for ( String men:come) { temp = men.substring(6,14); t = Integer.parseInt(temp); if(old > t){ old = t; oldNum = men; } } System.out.println(oldNum); } }
|
BufferedReader版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import java.io.*; import java.util.HashSet;
public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); HashSet<String> alumnus = new HashSet<>(); HashSet<String> come = new HashSet<>(); HashSet<String> alumnusCome = new HashSet<>(); int n = Integer.parseInt(reader.readLine()); for (int i = 0; i < n; i++) { alumnus.add(reader.readLine()); }
int m = Integer.parseInt(reader.readLine()),count = 0; String temp; for (int i = 0; i < m; i++) { temp = reader.readLine(); come.add(temp); if(alumnus.contains(temp)){ alumnusCome.add(temp); count++; } }
System.out.println(count); int old = Integer.MAX_VALUE,t; String oldNum = null; if(count>0){ come = alumnusCome; } for ( String men:come) { temp = men.substring(6,14); t = Integer.parseInt(temp); if(old > t){ old = t; oldNum = men; } } System.out.println(oldNum); reader.close(); } }
|