人口普查
这题目用java是会超时的,我提供java代码,自己对照去写c++,用c++最后一个测试点100ms左右,估计用java需要500-600ms,会超时,一般200ms的用java能过的可能性就比较小了,倒数第二个测试点如果出现段错误就是你的数组越界了,没有考虑全部错误的情况,输出0,最后一个测试点数据有点大,如果是错误,就是放进容器时的判断条件有错。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; class Person { public int year, month, day; public String name; public Person(int year, int month, int day, String name) { this.year = year; this.month = month; this.day = day; this.name = name; } } public class Main { public static int year, month, day; public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); Scanner cin = new Scanner(System.in); int n = cin.nextInt(), count = 0; for (int i = 0; i < n; ++i) { String name = cin.next(); String[] c = cin.next().split("/"); year = Integer.parseInt(c[0]); month = Integer.parseInt(c[1]); day = Integer.parseInt(c[2]); if (isCheck()) { list.add(new Person(year, month, day, name)); ++count; } } if (count > 0) { Collections.sort(list, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { if (o1.year != o2.year) { return o1.year - o2.year; } else if (o1.month != o2.month) { return o1.month - o2.month; } else { return o1.day - o2.day; } } }); System.out.println(count + " " + list.get(0).name + " " + list.get(count-1).name); } else { System.out.println(0); } } public static boolean isCheck() { if (year > 2014 || year < 1814) return false; else if (year == 2014) { if (month > 9) return false; else if (month == 9) { if (day > 6) return false; } } else if (year == 1814) { if (month < 9) return false; else if (month == 9) { if (day < 6) return false; } } return true; } }
========================================Talk is cheap, show me the code=======================================
CSDN博客地址:https://blog.csdn.net/qq_34115899