1. 程式人生 > >第八屆藍橋杯JavaB組-日期問題

第八屆藍橋杯JavaB組-日期問題

日期問題
小明正在整理一批歷史文獻。這些歷史文獻中出現了很多日期。小明知道這些日期都在1960年1月1日至2059年12月31日。
令小明頭疼的是,這些日期採用的格式非常不統一,有采用年/月/日的,有采用月/日/年的,還有采用日/月/年的。
更加麻煩的是,年份也都省略了前兩位,使得文獻上的一個日期,存在很多可能的日期與其對應。  

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。  
給出一個文獻上的日期,你能幫助小明判斷有哪些可能的日期對其對應嗎?

輸入
一個日期,格式是"AA/BB/CC"。  (0 <= A, B, C <= 9)  

輸入
輸出若干個不相同的日期,每個日期一行,格式是"yyyy-MM-dd"。多個日期按從早到晚排列。  

樣例輸入
02/03/04  

樣例輸出
2002-03-04  
2004-02-03  
2004-03-02  
資源約定:
峰值記憶體消耗(含虛擬機器) < 256M

CPU消耗  < 1000ms

import java.util.Iterator;
import java.util.Objects;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;


class dateTime implements Comparable<dateTime>{
    private int year, month, day;

    public dateTime(){}
    public dateTime(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int getYear() { return this.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 compareTo(dateTime other){
        if(this.year < other.getYear()) return -1;
        else if(this.year > other.getYear()) return 1;
        if(this.month < other.getMonth()) return -1;
        else if(this.month > other.getMonth()) return 1;
        if(this.day < other.getDay()) return -1;
        else if(this.day > other.getDay()) return 1;
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        dateTime dateTime = (dateTime) o;
        return year == dateTime.year && month == dateTime.month && day == dateTime.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }
}

public class Main{

    public static final int[] BASE = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    public static boolean judgeRunYear(int year) {
        return ((0 == year % 4 && 0 != year % 100) || 0 == year % 400);
    }

    public static void main(String[]  args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] strArr = in.readLine().split("/");
        in.close();
        int[] numArr = new int[3];
        numArr[0] = Integer.valueOf(strArr[0]);
        numArr[1] = Integer.valueOf(strArr[1]);
        numArr[2] = Integer.valueOf(strArr[2]);
        Set<dateTime> set = new TreeSet<dateTime>();
        /**
         * 解決排序的問題我是用的TreeSet的自帶的排序特效,但是這個特效需要去實現Comparable介面,
         * 重寫compareTo方法;
         *
         * 再者就是來看年的範圍了:
         * 1960~1999  &&  2000~2059 可以發現如果年後兩位是小於60的話,肯定前面是20,否則是19開頭
         * 接著就對輸入的這個數進行判斷是否符合三中日期的情況,如果符合就加入到Set中;
         * 這裡也要考慮到去重,還有閏年的2月問題;
         *
         * 感覺我的寫法有問題的童鞋,或者有更好的思路,歡迎在留言區留言!THANKS
         * */
        //yyyy-mm-dd
        int year, month, day;
        if(60 > numArr[0]) year = numArr[0] + 2000;
        else year = numArr[0] + 1900;
        month = numArr[1];
        day = numArr[2];
        if(0 < month && month < 13) {
            if((0<day&&day<=BASE[month])||(2==month&&judgeRunYear(year)&&0<day&&day<=BASE[month]+1)){
                set.add(new dateTime(year, month, day));
            }
        }

        //mm-dd-yyyy
        if(60 > numArr[2]) year = numArr[2] + 2000;
        else year = numArr[2] + 1900;
        month = numArr[0];
        day = numArr[1];
        if(0 < month && month < 13) {
            if((0<day&&day<=BASE[month])||(2==month&&judgeRunYear(year)&&0<day&&day<=BASE[month]+1)){
                set.add(new dateTime(year, month, day));
            }
        }

        //dd-mm--yyyy
        if(60 > numArr[2]) year = numArr[2] + 2000;
        else year = numArr[2] + 1900;
        month = numArr[1];
        day = numArr[0];
        if(0 < month && month < 13) {
            if((0<day&&day<=BASE[month])||(2==month&&judgeRunYear(year)&&0<day&&day<=BASE[month]+1)){
                set.add(new dateTime(year, month, day));
            }
        }

        Iterator<dateTime> it = set.iterator();
        //用迭代器的方式寫一下,熟悉一下;比forEach麻煩了點
        dateTime temp;
        while(it.hasNext()) {
            temp = it.next();
            System.out.println(temp.getYear() + "-" + temp.getMonth() + "-" + temp.getDay());
        }
    }
}