You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
//判断是不是质数
|
|
bool isPrime(int n) { //最快的方法
|
|
if (n < 2) return false;
|
|
if (n == 2 || n == 3) return true;
|
|
if (n % 6 != 1 && n % 6 != 5) return false;
|
|
for (int i = 5; i <= floor(sqrt(n)); i += 6)
|
|
if (n % i == 0 || n % (i + 2) == 0)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
//转换整数到字符串
|
|
string intToStr(int n) {
|
|
char str[256] = {0};
|
|
sprintf(str, "%d", n);
|
|
return str;
|
|
}
|
|
|
|
//是不是回文
|
|
bool isHuiWen(int n) {
|
|
if (n < 10) return false;
|
|
//将整数转为字符串
|
|
string s = intToStr(n);
|
|
//镜像反转
|
|
string s1 = s;
|
|
reverse(s1.begin(), s1.end());
|
|
//如果源串与镜像串一致,那么是回文串
|
|
if (s == s1) return true;
|
|
return false;
|
|
}
|
|
|
|
|
|
int main() {
|
|
int n;
|
|
cin >> n;
|
|
for (int i = 2; i < n; i++) {
|
|
if (isHuiWen(i) && isPrime(i)) {
|
|
cout << i << endl;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|