AcWing 466. 回文日期
原题链接
简单
作者:
m_math
,
2024-02-24 15:52:20
,
所有人可见
,
阅读 26
枚举所有回文日期然后判断是否合法
#include <bits/stdc++.h>
using namespace std;
int res;
int date1,date2;
int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x)
{
int year = x / 10000;
int month = x % 10000 / 100;
int day = x % 100;
if(!month || month > 12)
return false;
if(!day) return false;
if(month != 2)
{
if(day > days[month]) return false;
}
else if(month == 2)
{
int leap = (year % 4 == 0 && year % 100 || year % 400 == 0);
if(day > days[month] + leap)
return false;
}
return true;
}
int main()
{
cin >> date1 >> date2;
for(int i = 1000;i <= 9999;i++)
{
int date = i,x = i;
while(x)
{
date = date * 10 + x % 10;
x /= 10;
}
if(date >= date1 && date <= date2 && check(date))
res++;
}
cout << res << endl;
return 0;
}