纯模拟,时间稍微有点长
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int a,b,i1,i2;
int ans;
int main(){
int s[9],o,m;
cin >> a >> b;
for(int i = a;i <= b;++i){
i1 = i % 100;
i2 = i % 10000 / 100;
if(i2 > 12 || i2 == 0)
continue;
if(i2 == 1 || i2 == 3 || i2 == 5 || i2 == 7 || i2 == 8 || i2 == 10 || i2 == 12) o = 31;
else if(i2 == 2) o = 29;
else o = 30;
if(i1 > o || i1 == 0) continue;
m = i;
for(int l = 0;m > 0;++l){
s[l] = m % 10;
m = m / 10;
}
if(i2 <= 12 && i1 <= o && s[0] == s[7] && s[1] == s[6] && s[2] == s[5] && s[3] == s[4]) ans++;
}
printf("%d\n",ans);
return 0;
}
y总方法nice
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int data1,data2;
int res;
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int data){
int year = data / 10000;
int month = data % 10000 / 100;
int day = data % 100;
if(month == 0 || month > 12) return false;
if(day == 0 || month != 2 && day > days[month]) return false;
if(month == 2){
int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
if(day > 28 + leap) return false;
}
return true;
}
int main(){
scanf("%d%d",&data1,&data2);
for(int i = 1000;i < 10000;++i){
int data = i,x = i;
for(int i = 0;i < 4;++i) data = data * 10 + x % 10,x /= 10;
if(data >= data1 && data <= data2 && check(data)) res++;
}
printf("%d\n",res);
return 0;
}