题目描述
枚举
样例
import java.util.*;
public class Main{
/*
枚举+双指针 会TLE
static boolean check(int s){
String b = Integer.toString(s) ;
char[] a = b.toCharArray();
int i = 0 ,j = a.length - 1;
while(i <= j - 1){
if(a[i] != a[j]){
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
int e = sc.nextInt();
int res = 0;
for(int i = s; i <= e; i++){
if(check(i)) res++;
}
System.out.println(res);
}
*/
/*
回文串左右对称,因此可以只枚举左半边,这样只需枚举 0∼9999总共一万个数,然后判断:
1.整个八位数构成的日期是否合法;
2.是否在范围内
*/
static int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//%取后几位数 /就是去前几位数
static boolean check(int date) {
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month == 0 || month >= 13 || day == 0) return false;
if (month != 2 && day > months[month]) return false;
if (month == 2) {
boolean leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (day > 28 + (leap ? 1 : 0)) return false;
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int date1 = scanner.nextInt();
int date2 = scanner.nextInt();
int res = 0;
for (int i = 0; i < 10000; i++) {
//x和r都等于r
int x = i, r = i;
//通过循环4次,拼凑成完整的八位数
for (int j = 0; j < 4; j++) {
//r先乘于10,再加上x的最后一位
r = r * 10 + x % 10;
//去除x的最后一位
x /= 10;
}
//判断日期是否合法
if (r >= date1 && r <= date2 && check(r)) {
res++;
}
}
System.out.println(res);
}
}