AcWing 1229. 日期问题
原题链接
简单
作者:
马33
,
2021-04-04 09:18:49
,
所有人可见
,
阅读 2
Java 代码
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
static Set<String> set = new HashSet<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.next();
String[] a = s.split("/");
f(a[0], a[1], a[2]);
f(a[2], a[0], a[1]);
f(a[2], a[1], a[0]);
Object[] set2 = set.toArray();
Arrays.sort(set2);
for (int i = 0; i < set2.length; i++) {
System.out.println(set2[i]);
}
}
public static void f(String y, String m, String d) {
int year = Integer.parseInt(y);
int month = Integer.parseInt(m);
int day = Integer.parseInt(d);
if (year >= 60) {
year = 1900 + year;
} else {
year = 2000 + year;
}
if (month >= 1 && month <= 12) {
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day >= 1 && day <= 30) {
String s = year + "-" + ((month == 11) ? "" : "0") + month + "-" + (day >= 10 ? "" : "0") + day;
set.add(s);
}
} else if (month == 2) {
if (year % 4 == 0 && day >= 1 && day <= 29) {
String s = year + "-" + "0" + month + "-" + (day >= 10 ? "" : "0") + day;
set.add(s);
}
if (year % 4 != 0 && day >= 1 && day <= 28) {
String s = year + "-" + "0" + month + "-" + (day >= 10 ? "" : "0") + day;
set.add(s);
}
} else {
if (day >= 1 && day <= 31) {
String s = year + "-" + ((month == 10 || month == 12) ? "" : "0") + month + "-"
+ (day >= 10 ? "" : "0") + day;
set.add(s);
}
}
}
}
}