AcWing 466. 回文日期
原题链接
简单
作者:
不知名的fE
,
2024-11-23 14:55:52
,
所有人可见
,
阅读 1
import java.util.*;
//92200229
public class Main {
static int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int d1 = sc.nextInt(), d2 = sc.nextInt();
int res = 0;
for (int i = 1000; i < 10000; i++) {
int d = i, t = i;
for (int j = 0; j < 4; j++) {
d = d * 10 + t % 10;
t /= 10;
}
if (d1 <= d && d <= d2 && cheek(d)) res++;
}
System.out.println(res);
}
//判断日期是否合法
static boolean cheek(int d) {
int year = d / 10000;//取出年份
int mouth = d % 10000 / 100;//取出月份
int day = d % 100;//取出日期
if (mouth == 0 || mouth > 12) return false;
if (day == 0 || mouth != 2 && day > days[mouth]) return false;
if (mouth == 2) {//对2进行特判
boolean leap = year % 100 != 0 && year % 4 == 0 || year % 400 == 0;//判断是否是闰年
int t = leap ? 28 + 1 : 28;
if (day > t) return false;
}
return true;
}
}