将输入的日期看成数字判断 注意输入日期范围和答案范围(WA好多次的我)
合法日期判断借鉴了y总的板子 详情见注释,如有错误敬请批评指正。
C++ 代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check1(string n) //下一个回文日期
{
for(int i = 0, j = n.size() - 1; i < j; i++, j --)
if(n[i] != n[j])
return false;
return true;
}
bool check2(string n) //ABABBABA
{
if(n[0] == n[2] && n[2] == n[5] && n[5] == n[7] && n[1] == n[3] && n[3] == n[4] && n[4] == n[6] && n[0] != n[1])
return true;
return false;
}
bool check_valid(int year, int month, int day) //合法日期判断
{
if(month == 0 || month > 12) return false;
if(day == 0) return false;
if(month != 2)
{
if(day > days[month]) return false;
}
else //闰年特判
{
int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
if(day > 28 + leap) return false;
}
return true;
}
int main()
{
int date;
cin >> date;
int cnt = 0, flag = 0; //下一个回文日期和ABABBABA型日期 同时当做循环提前退出的条件
int year, month, day;
for(int i = date + 1; i <= 99999999; i ++)
{
year = i / 10000, month = i % 10000 / 100, day = i % 100; //取出年、月、日
if(!check_valid(year, month, day)) //如果当前枚举日期不合法 直接将日期+1 执行下次循环
continue;
if(!cnt && check1(to_string(i))) //下一个回文日期没有算出且该日期能构成回文日期
{
cnt = i;
}
if(!flag && check2(to_string(i))) //下一个ABABBABA型日期没有算出且该日期能构成回文日期
{
flag = i;
}
if(cnt && flag) break; //都被算出提前退出循环
}
cout << cnt << endl << flag << endl;
return 0;
}