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 |
读一个,给个答案,不需要把答案放一起统一输出In Reply To:大家帮我看看这个代码 为什么会超时呢?? Posted by:Lemke at 2009-11-08 11:18:54 > #include<stdio.h> > #include<math.h> > int prime(int x,int a[]) > { > int i,j,flag,k=0; > for(i=2;i<=x;i++) > { > flag=1; > for(j=2;j<=sqrt(i);j++) > { > if(i%j==0) > { > flag=0;break; > } > } > if(flag) > { > a[k]=i;k++; > } > } > return k; > } > int count(int x) > { > int a[170],sum[170]; > int i,j,done=0; > int n=prime(x,a); > for(i=0;i<n;i++) > { > sum[i]=0; > for(j=i;(j<n)&&(sum[i]<x);j++) > { > sum[i]=sum[i]+a[j]; > if(sum[i]==x) > { > done++; > break; > } > } > } > return done; > } > > > main() > { > int t[10],i=0; > int x,j; > while(x) > { > scanf("%d",&x); > t[i]=count(x);i++; > } > for(j=0;j<i-1;j++) > { > printf("%d\n",t[j]); > } > > > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator