AcWing 466. 回文日期
原题链接
简单
作者:
第九片海_2
,
2024-01-26 17:01:02
,
所有人可见
,
阅读 30
回文日期的日历写法
这题最直观的思路就是从起始日期枚举到截止日期,记录有多少回文日期
C++ 代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10;
int start,en;
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct date
{
int year;
int month;
int day;
};
date getdate(int x)//将数字转化为日期
{
int t=0,p=1;;
date res;
res.year=x/10000;
res.month=x%10000/100;
res.day=x%100;
return res;
}
bool checkyear(int x)//判断是不是闰年
{
if(x%400==0||x%4==0)return true;
return false;
}
int turn(date x)//将日期变化为数字
{
int res=0;
res+=x.year*10000;
res+=x.month*100;
res+=x.day;
return res;
}
void nextday(int &x)//枚举到下一天
{
date t=getdate(x);
t.day++;
if(checkyear(t.year))months[1]++;
if(t.day>months[t.month-1])
{
t.day=1;
t.month++;
if(t.month>12)
{
t.month=1;
t.year++;
}
}
months[1]=28;
x=turn(t);
}
bool check(int x)//判断是不是回文数
{
int a[8]={0};
for(int i=0;i<8;i++)
{
a[i]=x%10;
x/=10;
}
for(int i=0;i<4;i++)
if(a[i]!=a[7-i])
return false;
return true;
}
int main()
{
cin>>start>>en;
int ans=0;
while(start<=en)
{
if(check(start))ans++;
nextday(start);
}
cout<<ans<<endl;
return 0;
}