算法題練習系列之(二十一): 人口普查

--------------------------------------------------------------------------------------------------------------------------------------------------------

時間限制:1秒  空間限制:32768K  代碼長度限制 100 KB

--------------------------------------------------------------------------------------------------------------------------------------------------------

題目描述

某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程序,找出鎮上最年長和最年輕的人。
這裏確保每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過200歲的老人,
而今天是2014年9月6日,所以超過200歲的生日和未出生的生日都是不合理的,應該被過濾掉。
輸入描述:
輸入在第一行給出正整數N,取值在(0, 105];隨後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

--------------------------------------------------------------------------------------------------------------------------------------------------------

實現思路:

(1).用Man類存儲人的屬性(姓名、生日的年月日);
(2).用list存儲人口信息,存儲前判斷該生日是否合理,判斷方式就是年紀在0-200歲;

(3).對list進行升序排序;

(4).輸出list大小,以及第一個和最後一個元素的信息即可;

--------------------------------------------------------------------------------------------------------------------------------------------------------


package com.biyao.algorithm.niuke.a1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main_a1_018 {
    
    static class Man{
        String name;
        int year;
        int month;
        int day;
        public Man(String name,int year,int month,int day){
            this.name = name;
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }
    
    public static void main(String[] args) {
        
       
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
           int num = scan.nextInt();
           List<Man> list = new ArrayList<Man>();
           for (int i = 0; i < num; i++) {
               String name = scan.next();
               String birth = scan.next();
               String[] birthArr = birth.split("/");
               int year = Integer.parseInt(birthArr[0]);
               int month = Integer.parseInt(birthArr[1]);
               int day = Integer.parseInt(birthArr[2]);
               if(isReasonableBirthday(year,month,day)){
                   Man m = new Man(name,year,month,day);
                   list.add(m);
               }
           }
           Collections.sort(list, new Comparator<Man>(){
            @Override
            public int compare(Man o1, Man o2) {
                if(o1.year > o2.year){
                    return 1;
                }else if(o1.year == o2.year){
                    if(o1.month > o2.month){
                        return 1;
                    }else if(o1.month == o2.month){
                        if(o1.day > o2.day){
                            return 1;
                        }else if(o1.day == o2.day){
                            return 0;
                        }else{
                            return -1;
                        }
                    }else{
                        return -1;
                    }
                }else{
                    return -1;
                }
            }
           });
           System.out.println(list.size() + " " + list.get(0).name + " " + list.get(list.size()-1).name);
           
        }
    }
    
    
    public static boolean isReasonableBirthday(int year,int month,int day){
        if(year < 1814 || year > 2014){
            return false;
        }else if(year == 1814){
            if(month < 9){
                return false;
            }else if(month == 9){
                if(day < 6){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        }else if(year == 2014){
            if(month > 9){
                return false;
            }else if(month == 9){
                if(day > 6){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        }
        return true;
    }
    
}



發佈了66 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章