题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181
AC代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int prime[1000000];
int vis[10000001];
int number,cnt;
int limit;
void create_table()
{
limit = 1e6+200;
memset(vis,0,sizeof(vis)); ///先假设都是素数
number = 0;
for(int i = 2; i <= sqrt(limit); i++)
{
for(int j = 2*i; j < limit; j += i)
{
vis[j] = 1;
}
}
for(int i = 2; i < limit; i++)
{
if(vis[i]==0)
{
prime[++number] = i;
}
}
cnt = 0;
for(int i = 1; i <= number; i++)
{
if(vis[i]==0)
prime[cnt++] = prime[i];
}
}
int main()
{
create_table();
int n;
while(~scanf("%d",&n))
{
int pos = lower_bound(prime,prime+cnt,n)-prime;
printf("%d\n",prime[pos]);
}
return 0;
}