lanqiaoOJ 498 回文日期
//编写这么几个函数:
//1.从string转换为指定位数int的函数
//2.从int转换为指定位数string的函数
//3.判断是否为闰年的函数
//4.判断日期是否合法的函数
//5.判断字符串是否为回文日期的函数
//6.判断字符串是否为ABABBABA型回文的函数
#include<bits/stdc++.h>
using namespace std;
int s2i(string s){
int res=0;
for(int i=0;i<s.size();i++){
int x=s[i]-'0';
res=res*10+x;
}
return res;
}
//对于一个数字,如果没有数位的保障,当数字为各位数如月份中的9,应该用09来表示,那么在从整数转字符串的过程中, 就会导致字符串中的字符个数减少
string i2s(int x,int w){
string s;
while(x){
s+=(x%10)+'0';
x/=10;
}
while(s.length()<w){
s+='0';
}
reverse(s.begin(),s.end());
return s;
}
bool isleapyear(int year){
return(year%400==0||(year%4==0&&year%100!=0));
}
bool isok(int year,int month,int day){
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapyear(year)){
days[2]=29;
}
return day<days[month];
}
bool ispa1(string s){
for(int i=0;i<s.length()/2;i++){
if(s[i]!=s[s.length()-1-i])return false;
}
return true;
}
bool ispa2(string s){
if(!ispa1(s))return false;
return s[0]==s[2]&&s[1]==s[3];
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
string s;cin>>s;
int year=s2i(s.substr(0,4)),month=s2i(s.substr(4,2)),day=s2i(s.substr(6,2));
bool ans1=false,ans2=false;
for(int i=year;i<=9999;i++){
for(int j=1;j<=12;j++){
if(i==year&&j<month)continue;
for(int k=1;k<=31;k++){
if(i==year&&j==month&&k<=day)continue;
if(!isok(i,j,k))continue;
string date=i2s(i,4)+i2s(j,2)+i2s(k,2);
//这道题目不要担心,会不会在ABABBABA型的回文日期出现之前,并没有普通的回文日期,
//因为两个判断互不干扰,ABABBABA型回文串,也是回文串
//实在不行,两个大不了输出一样的字符串又不是不行
if(!ans1&&ispa1(date)){
cout<<date<<endl;
ans1=true;
}
if(!ans2&&ispa2(date)){
cout<<date<<endl;
ans2=true;
}
}
}
}
return 0;
}