思路:题目很简单,设年份为n以及m,我们只需要看从n-1到m+1有几个年份满足要求就行了
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
typedef long long ll;
typedef pair<int,int> PII;
int mon1[15] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int mon2[15] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//平
int a,b,a_1,b_1;
bool jud(int x){
if(x % 4 == 0 && x % 100 != 0) return true;
if(x % 400 == 0) return true;
return false;
}//判断平年闰年
int rever(int x){
int a,b,c,d;
a = x % 10;
x /= 10;
b = x % 10;
x /= 10;
c = x % 10;
x /= 10;
d = x % 10;
int k = a * 1000 + b * 100 + c * 10 + d;
return k;
}//反转年份
bool solve(int x){
int x1 = rever(x);
int num = x * 10000 + x1;
// cout << x << ' ' << x1 << '\n';
// cout << num << '\n';
if(jud(x) && num >= a && num <= b){
int q = x1 / 100;
int w = x1 % 100;
if(q > 12 || q < 1) return false;
if(w <= mon1[q] && w > 0) return true;
return false;
}
else if(!jud(x) && num >= a && num <= b){
int q = x1 / 100;
int w = x1 % 100;
if(q > 12 || q < 1) return false;
if(w <= mon2[q] && w > 0) return true;
return false;
}
return false;
}//判断反转后是否合格
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> a >> b;
a_1 = a / 10000;
b_1 = b / 10000;
int ans = 0;
for(int i = a_1 - 1; i <= b_1 + 1; i ++){
if(solve(i)){
ans ++;
}
}
cout << ans;
return 0;
}
当然也可以先把合格的回文日期先预处理存储起来再看输入的两个日期年份之间有几个满足要求的