藍橋杯第八屆省賽JAVA真題----日期問題
阿新 • • 發佈:2019-01-24
標題:日期問題
小明正在整理一批歷史文獻。這些歷史文獻中出現了很多日期。小明知道這些日期都在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及以上版本的特性。
思路:異常混亂的題目,限制條件非常多。看似簡單,實則陷阱重重
月份的範圍在0-12之間
天數受到月份的限制
2月的天數還受到閏年的限制
小明正在整理一批歷史文獻。這些歷史文獻中出現了很多日期。小明知道這些日期都在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,否則按無效程式碼處理。
月份的範圍在0-12之間
天數受到月份的限制
2月的天數還受到閏年的限制
輸出按順序,注意不能出現重複的如03/03/03 輸出一個2003-03-03
完整程式碼如下:
import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.Scanner; import java.util.Set; class Date { int year; int month; int day; public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } @Override public String toString() { // TODO Auto-generated method stub if (month < 10 || day < 10) { return year + "-0" + month + "-0" + day; } return year + "-" + month + "-" + day; } } public class Main { static int[] md = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; static String[] str = new String[3]; static Date[] date = new Date[6]; public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); str = s.split("/"); int a = Integer.valueOf(str[0]); int b = Integer.valueOf(str[1]); int c = Integer.valueOf(str[2]); // 年月日 date[0] = new Date(1900+a, b, c); date[1] = new Date(2000+a, b, c); // 月日年 date[2] = new Date(1900+c, a, b); date[3] = new Date(2000+c, a, b); // 日月年 date[4] = new Date(1900+c, b, a); date[5] = new Date(2000+c, b, a); // 排序,重寫Comparator介面的compare方法,保證按順序輸出 Arrays.sort(date, new Comparator<Date>() { @Override public int compare(Date d1, Date d2) { if (d1.year == d2.year) { if (d1.month == d2.month) { return (int) d1.day - d2.day; }return (int) d1.month - d2.month; } return (int) d1.year - d2.year; } }); // 避免重複 Set<String> set = new HashSet<String>(); for (int i = 0; i < date.length; i++) { if (vial(date[i]) && !set.contains(date[i].toString())) { set.add(date[i].toString()); System.out.println(date[i].toString()); } } } // 用於判斷日期是否符合規則,規則很多 private static boolean vial(Date d) { if (d.year < 1960 || d.year > 2059) { return false; } if (d.month <= 0 || d.month > 12) { return false; } if (d.year % 400 == 0 || d.year % 100 != 0 && d.year % 4 == 0) { if (d.month == 2) { return d.day >= 1 && d.day <= 29; } return d.day >= 1 && d.day <= md[d.month]; } else { return d.day >= 1 && d.day <= md[d.month]; } } }