Problem
水题
这个题的简便之处在于输入模式的固定,固定的八位数,固定前四位年份依次是月份和天。
因为一年的年份不会变,所以一年最多有一个回文数,只需要枚举一次即可,依次枚举判断即可,要注意闰年的判断,闰年分为普通闰年和世纪闰年
普通闰年的条件是
if (year % 4 == 0 && year % 100 != 0) return true;
世纪闰年:
if (year % 400 != 0) return true;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String date1 = br.readLine();
String date2 = br.readLine();
int year1 = stringToInt_4(date1);
int year2 = stringToInt_4(date2);
int res = 0;
for (int i = year1; i <= year2; i++) {
if (check(i)) res++;
}
pw.print(res);
pw.flush();
pw.close();
br.close();
}
public static int stringToInt_4(String s) {
return Integer.parseInt(String.valueOf(s.charAt(0))) * 1000 + Integer.parseInt(String.valueOf(s.charAt(1))) * 100
+ Integer.parseInt(String.valueOf(s.charAt(2))) * 10 + Integer.parseInt(String.valueOf(s.charAt(3)));
}
public static int reverse(int u) {
int res = 0;
int i = 3;
while (u != 0) {
int temp = u % 10;
u /= 10;
res += temp * Math.pow(10, i--);
}
return res;
}
public static boolean check(int year) {
int u = reverse(year);
int month = u / 100;
int day = u % 100;
if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (day >= 1 && day <= 29)
return true;
} else {
if (day >= 1 && day <= 28)
return true;
}
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day >= 1 && day <= 31)
return true;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day >= 1 && day <= 30)
return true;
}
return false;
}
}