AcWing 1356. 回文质数
原题链接
简单
作者:
atsy
,
2021-02-09 22:59:51
,
所有人可见
,
阅读 346
首先找出所有的奇数位回文数, 然后判断是否是质数, 跑的飞快
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
int a, b;
int check(int k) {
for (int i = 2; i <= k / i; i ++ ) {
if (k % i == 0) return 0;
}
return 1;
}
int main(void) {
cin >> a >> b;
if (a <= 11) {
for (int i = a; i <= 11 && b >= 11; i ++ ) {
if (check(i)) cout << i << '\n';
}
}
if (b >= 101){
for (int i = 1; i <= 999; i ++ ) {
string s = to_string(i);
string ss = s;
reverse(ss.begin(), ss.end());
for (int j = 0; j <= 9; j ++ ) {
string h = s;
h += j + '0';
h += ss;
int n = 0;
stringstream t;
t << h;
t >> n;
// cout << n << endl;
if (n > b) {
continue;
}
if (check(n) && n >= a) cout << n << '\n';
}
}
}
return 0;
}