一、刷题内容
蓝桥杯真题:《回文日期》
二、知识点
复习:
闰年判断:“四年一闰,百年不闰,四百年再闰”,既能被400整除,或者能被4整除但不能被100整除。
新知:
bool类型(第一次实际运用):函数可以用,参数也可以用,false==0,true==1,代码中的flag可以改为bool类型;
三、解题代码
#include <iostream>
using namespace std;
bool isLeap(int y)
{
return (y%4==0&&y%100!=0)||(y%400==0);
}
bool check(int i)
{
int year=i/10000;
int month=(i/100)%100;
int day=i%100;
if(month>12||month==0) return false;
if(day>31) return false;
if(month==2)
{
if(isLeap(year)&&day>29)
return false;
if(!isLeap(year)&&day>28)
return false;
}
if(month==4||month==6||month==9||month==11)
{
if(day>30) return false;
}
return true;
}
int main()
{
int a,b,c,d,e,f,g,h;
int n;
int flag=0;//bool flag=flase;
cin>>n;
for(int i=n+1;i<=99999999;i++)
{
a=i/10000000;
b=(i/1000000)%10;
c=(i/100000)%10;
d=(i/10000)%10;
e=(i/1000)%10;
f=(i/100)%10;
g=(i/10)%10;
h=i%10;
if(check(i))
{
if((a==h)&&(b==g)&&(c==f)&&(d==e)&&flag==0)
{
cout<<i<<endl;
flag++;//flag=true;
}
if((a==c)&&(c==f)&&(f==h)&&(b==d)&&(d==e)&&(e==g))
{
cout<<i;
break;
}
}
}
return 0;
}