2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2 日。因为如果将这个日期按 “yyyymmdd” 的格式写成一个 8 位数是 20200202,恰好是一个回文数。我们称这样的日期是回文日期。
有人表示 20200202 是 “千年一遇” 的特殊日子。对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021 年 12 月 2 日。
也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个 ABABBABA 型的回文日期:21211212 即 2121 年 12 月 12 日。算不上 “千年一遇”,顶多算 “千年两遇”。
给定一个 8 位数的日期,请你计算该日期之后下一个回文日期和下一个 ABABBABA 型的回文日期各是哪一天。
输入包含一个八位整数 NNN,表示日期。
对于所有评测用例,10000101≤N≤8999123110000101 \leq N \leq 8999123110000101≤N≤89991231,保证 NNN 是一个合法日期的 8 位数表示。
输出两行,每行 1 个八位数。第一行表示下一个回文日期,第二行表示下一个 ABABBABA 型的回文日期。
输入
20200202
输出
20211202
21211212
这里需要的是月的范围是[1,12],每月天数的范围要计算,计算这里要记住的是2月闰年的时是29天,其它时候是28天,其它月份小学的时候教过怎么数手指,握着手,数背面的突起和凹下的,从左到右边数。1月大,二月小,三月大,四月小,五月大,六月小、七月大、八月大、九月小、十月大、十一月小、十二月大。除了二月,其他的大的是31天,小的是30天。
import java.util.*;public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int N = 8;int[] nums = new int[N];//在此输入您的代码...while (scan.hasNext()) {int a = 0, b = 0;int code = scan.nextInt();for (int i = code + 1; i < 1e9; i++) {int m = i, c = 0;while (c < N) {nums[c++] = m % 10;m /= 10;}int year = nums[0] * 1000 + nums[1] * 100 + nums[2] * 10 + nums[3];int mouth = nums[4] * 10 + nums[5];int day = nums[6] * 10 + nums[7];if ((mouth == 0 || mouth > 12) || (day == 0 || day > getDay(year, mouth))) continue;if (verify(nums)) {if (a == 0) a = i;if (lr(nums)) {b = i;break;}}}System.out.println(a + "\n" + b);}scan.close();}private static boolean verify(int[] array) {int l = 0, r = array.length - 1;while (l <= r) {if (array[l++] != array[r--]) return false;}return true;}private static boolean lr(int[] array) {int l = 0;int a = array[0], b = array[1];if (a == b) return false;while (l < 4) {if (array[l] != a) return false;if (array[l + 1] != b) return false;l += 2;}while (l < 8) {if (array[l] != b) return false;if (array[l + 1] != a) return false;l += 2;}return true;}private static int getDay (int year, int mouth) {switch (mouth) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;}if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return 29;return 28;}
}