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 |
求大佬改(C++)#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <algorithm> using namespace std; int n,m,s,f[50005],top,a[50005]; bool x[1000005]; void dabiao() { int z=sqrt(n*1.0); for(int i=2;i<=z;i++) { int k=sqrt(i*1.0); bool falg=true; for(int j=2;j<=k;j++) { if(i%j==0) { falg=false; break; } } if(falg) a[++top]=i; } memset(x,true,sizeof(x)); if(m==1) x[1]=false; for(int i=1;i<=top;i++) { int j=m/a[i]; if(m%a[i]!=0) j++; if(a[i]>=m) j=2; for(;j<=n/a[i];j++) x[a[i]*j-m+1]=false; } for(int i=1;i<=n-m;i++) if(x[i]) f[++s]=i+m-1; } int main() { //freopen(".in","r",stdin); //freopen(".out","w",stdout); while (scanf("%d %d",&m,&n)==2) { dabiao(); int a=f[1],b=f[2],c=f[1],d=f[2]; for(int i=1;i<s;i++) { if(f[i+1]-f[i]<b-a) { a=f[i]; b=f[i+1]; } if(f[i+1]-f[i]>d-c) { c=f[i]; d=f[i+1]; } } if(s<2) printf("There are no adjacent primes.\n"); else printf("%d,%d are closest, %d,%d are most distant.\n",a,b,c,d); s=0; top=0; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator