百度-双素数-c++
2017-09-27 本文已影响0人
Jacinth
/*
* DoubleNum.cpp
*
* Created on: 2017年9月27日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <unordered_map>
using namespace std;
bool p[1000010];
void creatTable() {
memset(p, true, sizeof(p));
p[0] = false; //0不是素数
p[1] = false; //1不是素数
for (int i = 2; i * i < 1000000; i++) {
for (int j = 2; j * i < 1000000; j++) {
p[j * i] = false; //筛掉2,3,……的倍数
}
}
}
int reverse(int num) { //倒置素数
int *R = new int[1000000];
int count = 0;
while (num) {
R[count++] = num % 10;
num /= 10;
}
int out = 0;
for (int i = 0; i < count; i++) {
out = out * 10 + R[i];
}
delete R;
return out;
}
int main() {
creatTable();
int k;
while (cin >> k) {
int count = 0;
int result = -1;
for (int i = 10; i < 1000000; i++) {
if (p[i] && p[reverse(i)] && (i != reverse(i)))
count++;
if (count == k) {
result = i;
break;
}
}
cout << result << endl;
}
return 0;
}