Topic description
During the 2020 Chinese New Year, a special date caught everyone's attention: February 2, 2020. Because if this date is written in the format of "yyyymmdd" as an 8-digit number, it is 20200202, which happens to be a palindrome. We call such dates a palindrome date.
Some people say that 20200202 is a special day "once in a thousand years". Xiao Ming disagrees with this very much, because less than 2 years later will be the next palindrome date: 20211202, which is December 2, 2021.
Some people also said that 20200202 is not just a palindrome date, but a palindrome date of ABABABA type. Xiao Ming also disagrees with this, because in about 100 years, the next palindrome date of the ABABABA type will be encountered: 21211212, which is December 12, 2121. It's not "once in a thousand years", but at most "twice in a thousand years".
Given an 8-digit date, please calculate which day is the next palindrome date and the next palindrome date of type ABABABA after the date.
enter
The input contains an eight-digit integer NN representing the date.
For all evaluation cases, 10000101 ≤ N ≤ 89991231 10000101 \leq N \leq 8999123110000101≤N≤8 9 9 9 1 2 3 1 , which guarantees that N is an 8-digit representation of a valid date.
20200202
output
Output two lines, each with 1 octet. The first row represents the date of the next palindrome, and the second row represents the date of the next palindrome of type ABAABBABA.
20211202
21211212
C++
#include <iostream>
#include <cstring>
using namespace std;
int months[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断回文日期是否合法
bool check_valid1(int date) {
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month < 1 || month > 12) return false;
if (day == 0 || month != 2 && day > months[month]) return false;
if (month == 2) {
int leap = (year % 100 && year % 4 == 0) || (year % 400); // 闰年判断表达式
if (day > months[month]+leap) return false;
}
return true;
}
// 判断是否是ABABBABA
bool check_valid2(int date) {
string s = to_string(date);
if(s[0]!=s[2] || s[1]!= s[3] || s[0] == s[1]) return false;
return true;
}
int main() {
int date1;
cin >> date1;
int res1 = 0, res2 = 0;
for (int i = 1000; i < 10000; i++) {
int date = i, x = i;
while (x) date = date * 10 + x % 10, x /= 10;
if (date > date1 && check_valid1(date)) {
if (!res1)res1 = date;
if (check_valid2(date) && !res2) res2 = date;
}
}
cout << res1 << endl << res2 << endl;
return 0;
}