1e8到1e9所有回文数全筛出来放一个数组里,使用数位方法枚举每一位。然后在数组里二分左端点和右端点看这之间有多少数
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int l, r;
vector<int> ans;
for(int a=1;a<=9;a++){
for(int b=0;b<=9;b++){
for(int c=0;c<=9;c++){
for(int d=0;d<=9;d++){
for(int e=0;e<=9;e++){
int num = a + (int)(1e8) * a + b * 10 + b * (int)(1e7) + c * (int)(1e2) + c * (int)(1e6);
num += d * (int)(1e3) + d * (int)(1e5) + e * (int)(1e4);
ans.push_back(num);
}
}
}
}
}
sort(ans.begin(), ans.end());
while(cin >> l >> r){
int p1 = lower_bound(ans.begin(), ans.end(), l) - ans.begin();
int p2 = lower_bound(ans.begin(), ans.end(), r) - ans.begin();
int res = 0;
if(r == ans[p2]) res = p2 - p1 + 1;
else res = p2 - p1;
cout << res << '\n';
}
return 0;
}
交一发试试