顺着日期
public class Main {
public static void main(String[] args) {
int res = 0;
//2022年
int[] days = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
//31,28,31,30,31,30,31,31,30,31,30,31
//一三五七八十腊
//构造2022年每一天的日期yyyymmdd
StringBuffer date = new StringBuffer("2022");
for(int m=1;m<=12;m++) {
if(m<10) {
date.append(0);
}
date.append(m);
for(int d=1;d<=days[m-1];d++) {
if(d<10) {
date.append(0);
}
date.append(d);
if(isLegal(date.toString())) {
res++;
}
date.delete(6, 8);//20220101 -> 202201
}
date.delete(4, 8);//202201 -> 2022
}
System.out.println(res);
}
public static boolean isLegal(String date) {
//20220101
//202201
int l = 0;
int n = date.length();
while(l<=n-3) {
int t1 = (int)Integer.valueOf(date.substring(l,l+1));//2
int t2 = (int)Integer.valueOf(date.substring(l+1,l+2));//0
int t3 = (int)Integer.valueOf(date.substring(l+2,l+3));//2
if(t1 == (t2-1) && t2 == (t3-1)) {
return true;
}
l++;
}
return false;
}
}