这个题枚举所有的天数显然是不行的,和写日历一样比较麻烦,我们可以枚举所有的年份然后逆过来判断是否合法即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int t[4];
int main(){
int n,flag1=0,flag=0;
cin>>n;
n/=10000;
for(int i=n;i<=8999;i++){//枚举月份
if((i%4==0&&i%100!=0)||i%400==0)a[2]=29;//判断闰年
else a[2]=28;
int temp=i,k=0;
for(int j=0;j<4;j++){
t[j]=temp%10;
temp/=10;
}
// for(int j=0;j<4;j++)
int month=t[0]*10+t[1];
if(month>12||t[2]*10+t[3]>a[month])continue;//月份不符合就会筛掉
if(!flag){//回文日期
flag=1;
for(int j=3;j>=0;j--){
cout<<t[j];
}
for(int j=0;j<4;j++){
cout<<t[j];
}
cout<<endl;
}
if(i%10==i/100%10&&i/1000==i/10%10){//ababbaba型
flag1=1;
for(int j=3;j>=0;j--){
cout<<t[j];
}
for(int j=0;j<4;j++){
cout<<t[j];
}
}
if(flag&&flag1)break;
}
return 0;
}