AcWing 466. 回文日期
原题链接
简单
作者:
hegehog
,
2020-07-09 16:15:21
,
所有人可见
,
阅读 554
C++代码
#include <iostream>
#include <cstdio>
using namespace std;
int m, n;
int res;
bool judge_date(int year, int month, int day)
{
int k[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(month < 1 || month > 12) return false;
if(day < 1 || day > k[month]) return false;
if(!((year % 100 != 0 && year % 4 == 0) || year % 400 == 0))
if(month == 2 && day > 28) return false;
return true;
}
//枚举所有回文日期:
int main()
{
cin >> m;
cin >> n;
for(int i = m / 10000; i <= n / 10000; i ++)
{
int x = i;
int t = x;
while(x){
t *= 10;
t += x % 10;
x /= 10;
}
int year = i, month = (t / 100) % 100, day = t % 100;
if(t >= m && t <= n)
if(judge_date(year, month, day)) res ++;
}
cout << res << endl;
return 0;
}