AcWing 466. 回文日期
原题链接
简单
作者:
Fairy2.0
,
2025-01-04 15:38:13
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
using namespace std;
int Month[13] = {0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
bool check_valid(int eddate , int d){
if (d > eddate) return false;
int year = d / 10000;
int month = d / 100 % 100;
int day = d % 100;
//cout << year << " " << month << " " << day << endl;
if (month < 1 || month > 12 || day < 1) return false;
if (month != 2){
if (Month[month] < day)
return false;
}
else{
int t = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
if (day > Month[2] + t) return false;
}
return true;
}
int main (){
int stdate = 0;
int eddate = 0;
cin >> stdate >> eddate;
int styear = stdate / 10000; //得到前面4位
int edyear = eddate / 10000;
int res = 0;
//按道理说一年中最多有一个回文日期
for (int i = styear;i <= edyear;i++){
//先组成要的回文日期
int d = i;
int x = i;
while(x){
d = d * 10 + x % 10;
x = x / 10;
}
// cout << d << endl;
//查看回文日期是不是符合的
if (d >= stdate && check_valid(eddate , d)) res++;
}
cout << res << endl;
return 0;
}