#include <bits/stdc++.h>
using namespace std;
int months[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31};
bool check(int date)
{
int year = date / 10000;//从date中分离出年
int month = date % 10000 / 100;//从date中分离出月
int day = date % 100;//从date中分离出日
if (!day || month <= 0 || month > 12) return false;//月份和日期为0不合法
if (month != 2 && day > months[month]) return false;//日期不合法
if(month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400== 0)) {//判断平闰年
if (day > 29) return false;//闰年日期不合法
} else {
if (day > 28) return false;//平年日期不合法
}
}
return true;
}
//判断回文数
bool check1(string s)
{
//判断长度
int len = s.size();
for(int i = 0,j = len-1 ; i<j;i++,j--)
{
if(s[i] != s[j]) return false;
}
return true;
}
//判断ABABBABA
bool check2(string s)
{
if (check1(s)) {//先确定是否是回文数
if (s[0] != s[2] || s[1] != s[3] || s[0] == s[1])
return false;//判断是否是ABABBABA型
return true;
}
return false;
}
int main()
{
int date,flag = 0;
cin >> date;
for(int i = date+1;;i++)
{
if(check(i))//判断日期是否合法
{
string s = to_string(i);//将数字转化为字符串
if (check1(s) && !flag){
cout << i << endl;
flag = 1;
}
if(check2(s))//判断是否是ABABBABA
{
cout << i << endl;
return 0;
}
}
}
return 0;
}
复习回文日期
于 2024-04-11 22:11:01 首次发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)