回文数
判断回文数的模板
主要思路就是通过将数字反过来组合成新的一个数和原来的数进行比较
//判断回文数
bool isHWS(int num) {
int temp=num,ans=0;
while (temp!=0) {
ans = ans*10+temp%10;
temp /= 10;
}
if(ans == num) {
return true;
}else{
return false;
}
}
例题是[P1217 USACO1.5]回文质数 Prime Palindromes - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool prime[100000005];
//用埃氏筛法生成质数表
void ai(int b) {
for (int i = 2; i <= 100000001; ++i) prime[i] = 1;
int n = sqrt(b);
for (int i = 2; i <= n; ++i) {
if(prime[i])
for (int j = i*2; j <= b; j += i) {
prime[j] = 0;
}
}
}
//判断回文数
bool isHWS(int num) {
int temp=num,ans=0;
while (temp!=0) {
ans = ans*10+temp%10;
temp /= 10;
}
if(ans == num) {
return true;
}else{
return false;
}
}
int main() {
ll a,b;
cin >> a >> b;
if (b>=10000000) {
b=9999999;
}
ai(b);
if(a>b)
return 0;
if(a%2 == 0) a++;
for (int i = a; i <= b; i += 2) {
if(prime[i] && isHWS(i)) //点睛之笔
cout << i << endl;
}
return 0;
}