1028 人口普查java答案
这里用了输入流,并且继承了comparable的类进行排序,但是最后一个测试点仍然超时,暂时不清楚怎么解决
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
int times=Integer.parseInt(bufferedReader.readLine());
List<Person> people=new ArrayList<Person>();
for(int i=0;i<times;i++) {
String[] data=bufferedReader.readLine().split(" ");
String[] time=data[1].split("/");
int year=Integer.parseInt(time[0]);
int month=Integer.parseInt(time[1]);
int day=Integer.parseInt(time[2]);
Person person=new Person(data[0],year,month,day);
int age=person.getAge();
// 年龄在0到200之间
if(age>0&&age<200) {
people.add(person);
}else if(age==0||age==200) {
// 年龄为0的和200的必须月份等于9,日期等于6
if(month==9&&day==6) {
people.add(person);
}
}
}
Collections.sort(people);
if(people.size()!=0) {
System.out.print(people.size()+" "+people.get(people.size()-1).getName()+" "+people.get(0).getName());
}else {
System.out.print(0);
}
}
}
class Person implements Comparable<Person>{
private String name;
private int year;
private int month;
private int day;
private int age;
public Person(String name,int year,int month,int day) {
this.name=name;
this.year=year;
this.month=month;
this.day=day;
if(month<9) {
age=2014-year;
}else if(month>9) {
age=2014-year-1;
}else {
if(day<=6) {
age=2014-year;
}else if(day>6) {
age=2014-year-1;
}
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=age;
}
@Override
public int compareTo(Person person) {
return this.age-person.age;
}
}