题目描述
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 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
运行限制
- 最大运行时间:1s
- 最大运行内存: 256M
解题思路:
这是一道编程设计题,该题可以通过暴力求解进行解答并通过测试系统的测评。首先需要设计输入输出的格式,正确取出对应的AA、BB以及CC,然后便是对日历进行遍历,这个时候需要注意判断闰平年 (实际是注意判断二月的天数),然后以当前日期为准去迎合你的输入值,假设输入值的AA为年的话,输入值的BB就只能是月,CC就只能是日;假设输入值的CC是年的话,这个时候可以是AA为月BB为日,也可以是AA为日BB为月,这里需要一个判断。最后是需要对输出进行控制,月和日这两个格式若是一位数的时候需要前补 0,这就不能直接用 month 和day 进行输出,而是需要在原生接收输入的字符串中去取子串进行输出才能前补 0。该算法的 Java 代码实现如下:
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String date = scan.next();// "AA/BB/CC"
int AA = Integer.parseInt((String) date.subSequence(0, 2));
int BB = Integer.parseInt((String) date.subSequence(3, 5));
int CC = Integer.parseInt((String) date.subSequence(6, 8));
date.subSequence(0, 1);
int year, month, day;
for (year = 1960; year <= 2059; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {// 闰年,二月29天
for (month = 1; month <= 12; month++) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
|| month == 12) {
for (day = 1; day <= 31; day++) {// 年月日、月日年、日月年
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
} else if (month == 2) {
for (day = 1; day <= 29; day++) {
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
} else {
for (day = 1; day <= 30; day++) {
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
}
}
} else {// 平年,二月28天
for (month = 1; month <= 12; month++) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
|| month == 12) {
for (day = 1; day <= 31; day++) {
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
} else if (month == 2) {
for (day = 1; day <= 28; day++) {
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
} else {
for (day = 1; day <= 30; day++) {
if (year % 100 == AA && month == BB && day == CC) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(6, 8));
} else if (year % 100 == CC && month == AA && day == BB) {
System.out.println(year + "-" + date.subSequence(0, 2) + "-" + date.subSequence(3, 5));
} else if (year % 100 == CC && day == AA && month == BB) {
System.out.println(year + "-" + date.subSequence(3, 5) + "-" + date.subSequence(0, 2));
}
}
}
}
}
}
scan.close();
}
}