问题描述
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
比如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
2004-02-03
2004-03-02
数据规模和约定
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
CPU消耗 < 1000ms
思路:
1、建立代表每个月天数的数组便于判断
2、判断是否是闰年的函数
3、根据年月日的位置分别进行判断,并且将符合条件的字符串储存
4、排序后输出字符串
注意:
1、防止月份和天数出现非法数据需要先对其进行判断。如00
2、可能会出现相同的输出结果 例如02/02/02 ,当输出结果相同的时候只输出一次
3、闰年的2月有29天
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int[] day = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static boolean isrun(String year) {
int year1 = Integer.parseInt(year);
if (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String[] str = str1.split("/");
String pr = "";
String year = str[0];
if (Integer.parseInt(str[0]) >= 60) {
year = "19" + year;
} else {
year = "20" + year;
}
if (isrun(year)) {
day[1] = 29;
} else {
day[1] = 28;
}
if (Integer.parseInt(str[1]) >= 1 && Integer.parseInt(str[1]) <= 12 && Integer.parseInt(str[2]) > 0) {
if (day[Integer.parseInt(str[1]) - 1] >= Integer.parseInt(str[2])) {
String tem = year + "-" + str[1] + "-" + str[2];
if (-1 == pr.indexOf(tem)) {
pr += tem + " ";
}
}
}
year = str[2];
if (Integer.parseInt(str[0]) >= 60) {
year = "19" + year;
} else {
year = "20" + year;
}
if (isrun(year)) {
day[1] = 29;
} else {
day[1] = 28;
}
if (Integer.parseInt(str[0]) >= 1 && Integer.parseInt(str[0]) <= 12 && Integer.parseInt(str[1]) > 0) {
if (day[Integer.parseInt(str[0]) - 1] >= Integer.parseInt(str[1])) {
String tem = year + "-" + str[0] + "-" + str[1];
if (-1 == pr.indexOf(tem)) {
pr += tem + " ";
}
}
}
if (Integer.parseInt(str[1]) >= 1 && Integer.parseInt(str[1]) <= 12 && Integer.parseInt(str[0]) > 0) {
if (day[Integer.parseInt(str[1]) - 1] >= Integer.parseInt(str[0])) {
String tem = year + "-" + str[1] + "-" + str[0];
if (-1 == pr.indexOf(tem)) {
pr += tem + " ";
}
}
}
String[] pri = pr.split(" ");
Arrays.sort(pri);
for (String s : pri) {
System.out.println(s);
}
}
}