| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
wa??#include<stdio.h>
//用筛选法求素数
bool p[1299720];
int main()
{
int k,ans,i,i2;
for(i=0;i<=1299709;i+=2){ p[i]=0; }
for(i=1;i<=1299709;i+=2){ p[i]=1; }
p[2]=1;p[1]=0;
for(i=3;i<=1000;i+=2)
{ if(p[i]==1)
{i2=i+i; k=i2+i;
while(k<=1299709)
{p[k]=0;k+=i2;}
}
}
while(scanf("%d",&k)&&k!=0)
{
if(k==1)
{
printf("0\n");
continue;
}
if(p[k]==1)
{
printf("0\n");
continue;
}
else
{
ans=1;
for(i=k-1;;i--)
{
if(p[i]==0)
ans++;
else
break;
}
for(i=k+1;;i++)
{
if(p[i]==0)
ans++;
else
break;
}
printf("%d\n",ans+1);
}
}
return 0;
}/*10
11
27
2
492170
0*/
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator