原题链接:蓝桥杯 回文日期
emmmm这道题,就是有那么一点点坑。。。
首先要判断数字是否满足日期格式(原来闰年2月才是29天,非闰年2月是28天,我给记反了。。。)
然后肯定很多人看到对于所有评测用例,10000101≤N≤89991231
,就设了日期不能超过89991231
,反而,人家说的是输入日期。。。。 代码一直过不了,实在不解,把while(n1<=89991231)
改成while(1)
就行了。。害
#include <bits/stdc++.h>
using namespace std;
bool fun1(string s)
{
for(int i=0;i<4;i++)
{
if(s[i]!=s[7-i])
return false;
}
return true;
}
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool fun2(string s)
{
string s1=s.substr(0,2);
string s2=s.substr(2,2);
string s3=s.substr(4,2);
string s4=s.substr(6,2);
reverse(s3.begin(),s3.end());
reverse(s4.begin(),s4.end());
if(s1==s2 && s2==s3 && s3==s4)
return true;
else
return false;
}
bool isrun(int y)
{
if((y%4==0 && y%100!=0) || (y%400==0))
return true;
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
string str;
int n;
cin>>n;
int f1=0,f2=0;
int n1=n;
int year,month,day;
while(1)
{
year=n1/10000;
month=n1/100%100;
day=n1%100;
if(month==12 && day==31)
{
year++; month=1; day=1;
}
else if(isrun(year) && month==2)
{
if(day==29)
{
month=3; day=1;
}
else
day++;
}
else if(!(isrun(year)) || (isrun(year) && month!=2) )
{
if(day==a[month])
{
month++; day=1;
}
else
day++;
}
if(month>=1 && month<=9)
{
n1=year*10000+(10+month)*100+day-1000;
}
else
{
n1=year*10000+month*100+day;
}
stringstream ss;
ss<<n1;
string s=ss.str();
if(fun1(s) && f1==0)
{
f1=1;
cout<<n1<<endl;
}
if(fun1(s) && fun2(s))
{
f2=1;
cout<<n1<<endl;
}
if(f1==1 && f2==1)
{
break;
}
}
return 0;
}