Java實現PAT乙級1028人口普查
阿新 • • 發佈:2019-01-27
只通過了一個測試點,找不出錯誤,想請問下是哪裡不對啊?
1028 人口普查(20)(20 分)
某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程式,找出鎮上最年長和最年輕的人。
這裡確保每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過200歲的老人,而今天是2014年9月6日,所以超過200歲的生日和未出生的生日都是不合理的,應該被過濾掉。
輸入格式:
輸入在第一行給出正整數N,取值在(0,
10^5^];隨後N行,每行給出1個人的姓名(由不超過5個英文字母組成的字串)、以及按“yyyy/mm/dd”(即年/月/日)格式給出的生日。題目保證最年長和最年輕的人沒有並列。輸出格式:
在一行中順序輸出有效生日的個數、最年長人和最年輕人的姓名,其間以空格分隔。
輸入樣例:
5 John 2001/05/12 Tom 1814/09/06 Ann 2121/01/30 James 1814/09/05 Steve
1967/11/20輸出樣例:
3 Tom John
程式程式碼
import java.util.Scanner;
public class no1028 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in );
int num=in.nextInt();
int olderIndex=0;
int youngerIndex=0;
int countLegal=0;
person[] people=new person[num];
for(int i=0;i<num;i++)
{
String name=in.next();
String[] temp=in.next().split("/");
people[i]=new person(name,temp[0 ]+temp[1]+temp[2]);
}
in.close();
for(int i=0;i<num;i++)
{
if(people[i].isLegal()==false)
continue;
countLegal++;
if(people[i].isOlder(people[olderIndex]))
olderIndex=i;
if(people[i].isYounger(people[youngerIndex]))
youngerIndex=i;
}
System.out.print(countLegal+" "+people[olderIndex].name+" "+people[youngerIndex].name);
}
}
class person
{
String name;
String date;
public person() {}
public person(String name, String date) {
super();
this.name = name;
this.date = date;
}
public boolean isYounger(person person) {
int temp=Integer.parseInt(date);
if(temp<Integer.parseInt(person.date))
return true;
else
return false;
}
public boolean isOlder(person person) {
int temp=Integer.parseInt(date);
if(temp>Integer.parseInt(person.date))
return true;
else
return false;
}
boolean isLegal()
{
int temp=Integer.parseInt(date);
if(temp>20140906||20140906-temp>2000000)
return false;
else
return true;
}
}
測試截圖