PAT Class B 1028. If you don’t have AC, don’t be stubborn. Just use the right method. PAT is still very unfriendly to Java.

1028 Census (20 points)

A town conducted a census and obtained the birthdays of all residents. Now please write a program to find the oldest and youngest people in the town.

This ensures that each entered date is legal, but not necessarily reasonable - let's say it is known that there are no old people over 200 years old in the town, and today is September 6, 2014, so birthdays over 200 years old and not yet The birth date is unreasonable and should be filtered out.

Input format:

The input gives a positive integer N in the first line , with a value in (0,105

]; followed by N lines, each line gives the name of a person (a string consisting of no more than 5 English letters), and the yyyy/mm/ddbirthday in the format of (i.e. year/month/day). The question ensures that there is no tie between the oldest and youngest person.

Output format:

Output the number of valid birthdays, the names of the eldest and the youngest, in order on one line, separated by spaces.

Input example:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
     
    

Output sample:

3 Tom John

Question analysis:
Pitfall: Test point 3 is a situation where all data are unreasonable, just output 0 directly.

There is no need to convert the date into the Date data type, it can be processed directly as a string, using the compareTo method of the string, such as "1814/09/06" < "2001/05/12".

Not AC code:

 import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
    
    
    public static void main(String[] args) throws Exception{
    
    
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(br.readLine());
        //分别存放临时名字,临时日期,年少名字,年长名字
        String name,date,maxname="",minname="",maxdate="1814/09/06",mindate="2014/09/06";
        int count=0;//记录满足范围的个数
        for(int i=0;i<N;i++){
    
    
            String[] str=br.readLine().split(" ");
            name=str[0];date=str[1];//临时名字,临时日期
            //date必须满足这个范围
            if(date.compareTo("2014/09/06")<=0&&date.compareTo("1814/09/06")>=0){
    
    
                count++;//记录满足范围的个数
                //寻找年少者,这里要是不理解的话好好体会体会,主要是符号,>还是<
                if(maxdate.compareTo(date)<0){
    
    
                    maxdate=date;
                    maxname=name;
                }
                //寻找年长者
                if(mindate.compareTo(date)>0){
    
    
                    mindate=date;
                    minname=name;
                }
            }
        }
        
        //如果count=0,直接输出count
        System.out.print(count);
        if(count!=0){
    
    
            System.out.printf(" "+minname+" "+maxname);
        }
    }
}

I think this method is quite simple. You can also convert all dates into int type and compare the size relationship between 18140906, 20140906 and the given data. This method still times out at the last test point.

For more topic analysis, please follow the public account Algorithm Baby

Guess you like

Origin blog.csdn.net/CSDN_Lrcx/article/details/116168330