以下是个人解析认知,拿不到满分,只能拿到75分,有大佬能看出问题来帮忙在评论区解答以下。
从题目中我们可以了解到,我们做这个题用不到太高深的算法,只需要简单的了解条件即可:
1.闰年判断,以及闰年中的2月的天数问题(因为只有2月天数会变嘛)
2.年数合法判断,保证在题目要求的年数范围内
3.月份判断,每个月的天数是否合法,月数是否越界
4.最后输出的值按顺序输出并且不能出现相同的
import java.util.*;
import java.io.*;
/*
年/月/日
月/日/年
日/月/年。
1960年1月1日 到 2059年12月31日
*/
public class Main {
//要判断闰年
public static void main(String [] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] date = br.readLine().split("/");
String date1 = time1(date);
String date2 = time2(date);
String date3 = time3(date);
//先自动去重
HashSet<String> hs = new HashSet<>();
hs.add(date1);
hs.add(date2);
hs.add(date3);
LinkedList<String> ls = new LinkedList<>();
for(String s : hs) {
if(!isTrue(s)) {
//答案不对
ls.add(s);
}
}
hs.removeAll(ls);
//输出结果要排序
TreeSet<String> ts = new TreeSet<>(hs);
for(String s : ts) {
System.out.println(s);
}
}
//判断
static boolean isTrue(String s) {
String year = s.substring(0,4);
String month = s.substring(5,7);
String day = s.substring(8,10);
if(day.charAt(0) == '0') {
if(day.charAt(1) == '0') return false;
day = ""+day.charAt(1);
}
if(month.charAt(0) == '0') {
if(month.charAt(1) == '0') return false;
month = ""+month.charAt(1);
}
if(Integer.parseInt(month) > 12) {
return false;
}
//判断闰年和2月
if((Integer.parseInt(year) % 4 != 0 || Integer.parseInt(year) % 100 == 0
|| Integer.parseInt(year) % 400 != 0) && Integer.parseInt(month) == 2 && Integer.parseInt(day) > 28 ) {
//答案不对
return false;
}
//判断月份的对应日期
if((month.equals("1")||month.equals("3")||month.equals("5")||month.equals("7")||month.equals("8")||month.equals("10")
||month.equals("12")) && Integer.parseInt(day) > 31) {
return false;
}
if((month.equals("4")||month.equals("6")||month.equals("9")||month.equals("11")) && Integer.parseInt(day) > 30) {
return false;
}
return true;
}
//年-月-日
static String time1(String[] date) {
String year = date[0];
String month = date[1];
String day = date[2];
if(year.charAt(0) == '0') {
//证明肯定是20年的
year = "20" + year;
return year+"-"+month+"-"+day;
}
//为20年时
if(Integer.parseInt(year) <= 59) {
year = "20" + year;
return year+"-"+month+"-"+day;
}else {
//证明为19年
year = "19" + year;
return year+"-"+month+"-"+day;
}
}
//月-日-年
static String time2(String[] date) {
String month = date[0];
String day = date[1];
String year = date[2];
if(year.charAt(0) == '0') {
//证明肯定是20年的
year = "20" + year;
return year+"-"+month+"-"+day;
}
//为20年时
if(Integer.parseInt(year) <= 59) {
year = "20" + year;
return year+"-"+month+"-"+day;
}else {
//证明为19年
year = "19" + year;
return year+"-"+month+"-"+day;
}
}
//日-月-年
static String time3(String[] date) {
String day = date[0];
String month = date[1];
String year = date[2];
if(year.charAt(0) == '0') {
//证明肯定是20年的
year = "20" + year;
return year+"-"+month+"-"+day;
}
//为20年时
if(Integer.parseInt(year) <= 59) {
year = "20" + year;
return year+"-"+month+"-"+day;
}else {
//证明为19年
year = "19" + year;
return year+"-"+month+"-"+day;
}
}
}