题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181
代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
using namespace std;
const int MAXN = 10000010;
bool com[MAXN];
int primes, prime[MAXN / 10];
int num[MAXN];
void solve(int n)
{
primes = 0;
memset(com, false, sizeof(com));
com[0] = com[1] = true;
for (int i = 2; i <= n; ++i)
{
if (!com[i])
{
prime[++primes] = i;
num[i] = primes;
}
for (int j = 1; j <= primes && i*prime[j] <= n; ++j)
{
com[i*prime[j]] = true;
if (!(i % prime[j]))
break;
}
}
}
int main()
{
solve(1000010);
int n;
while (cin >> n)
{
while (!(!com[n] && !com[num[n]]))
n++;
cout << n << endl;
}
return 0;
}