AcWing 1229. 日期问题_Java
原题链接
简单
作者:
差一点睡死了
,
2021-02-18 17:21:07
,
所有人可见
,
阅读 358
package lanqiao;
import java.util.Scanner;
public class _1229_日期问题 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] str = sc.next().split("/");
int a=Integer.parseInt(str[0]);
int b=Integer.parseInt(str[1]);
int c=Integer.parseInt(str[2]);
for (int date= 19600101; date <=20591231; date++) {
int year=date/10000,month=date%10000/100, day = date % 100;
if(check_valid(year,month,day)) {
if (year % 100 == a && month == b && day == c || //年月日
month == a && day == b && year % 100 == c || //月年日
day == a && month == b && year % 100 == c) { //日月年
String month1="",day1="";
if(month<10){
month1="0"+String.valueOf(month);
}else {
month1=String.valueOf(month);
}
if(day<10){
day1="0"+String.valueOf(day);
}else {
day1=String.valueOf(day);
}
System.out.println(year+"-"+month1+"-"+day1);
}
}
}
}
static boolean check_valid(int year,int month,int day) {
int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month < 1 || month > 12) return false;
if(day <= 0) return false;
if(month!= 2)//不是二月
{
if(day > days[month]) return false;
}else{
int leap=0;
//闰年29天
if((year%100!=0&&year%4==0)||(year%400==0)){
leap =1;
}else{
leap=0;
}
if(day>(28+leap)) return false;
}
return true;
}
}