日期問題

/*
標題:日期問題

小明正在整理一批歷史文獻。這些歷史文獻中出現了不少日期。小明知道這些日期都在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


請嚴格按要求輸出,不要多此一舉地打印相似:「請您輸入...」 的多餘內容。

全部代碼放在同一個源文件中,調試經過後,拷貝提交該源碼。
不要使用package語句。不要使用jdk1.7及以上版本的特性。
主類的名字必須是:Main,不然按無效代碼處理。


*/
package test;
import java.util.*;
public class 日期問題 {
    static boolean leap(int m){//是否爲閏年
        if(m%4==0&&m%100!=0||m%400==0)
            return true;
        return false;
    }
    public static void main(String arg[]){
    Scanner input=new Scanner(System.in);
    String str=input.nextLine();
    int a=Integer.parseInt(str.substring(0, 2));
    int b=Integer.parseInt(str.substring(3, 5));
    int c=Integer.parseInt(str.substring(6, 8));
    int[] z=new int[6];
    z[0]=(2000+a)*10000+100*b+c;//將全部可能存至數組
    z[1]=(1900+a)*10000+100*b+c;
    z[2]=(2000+c)*10000+100*a+b;
    z[3]=(2000+c)*10000+100*b+a;
    z[4]=(1900+c)*10000+100*a+b;
    z[5]=(1900+c)*10000+100*b+a;
    int[] daycount=new int[]{31,29,31,30,31,30,31,31,30,31,30,31};
    Arrays.sort(z);
    for(int i=0;i<6;i++){
        int year=z[i]/10000;
        int month=z[i]%10000/100;
        int day=z[i]%100;
        if(year<1960||year>2059)continue;
        if(month<1||month>12)continue;
        if(leap(year))daycount[1]=29;
        else daycount[1]=28;
        if(day<1||day>daycount[month-1])continue;
        StringBuffer string=new StringBuffer(z[i]+"");
        string.insert(4, "-");
        string.insert(7,"-");
        System.out.println(string);
    }
    }
    
}
相關文章
相關標籤/搜索