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 |
做了好多关于素数的题,这个算是比较综合的,呵呵~~~~#include<iostream> using namespace std; void creat_prime(int *prime,int n) { int flag=2; int t=3; int i,j; for(i=7;i<n;i+=flag) { flag=6-flag; for(j=0;prime[j]*prime[j]<i;j++) { if(i%prime[j]==0)break; } if(prime[j]*prime[j]>i) { prime[t]=i; t++; } } } int main() { int i,j,k,n,sum,flag; int prime[2048]={2,3,5,0}; creat_prime(prime,10000); while(cin>>n,n) { flag=0; for(i=1;i<=n/2;i++) for(j=0; ;j++) { sum=0; for(k=j;k<i+j;k++) { sum+=prime[k]; if(sum>n)break; } if(sum==n) flag++; if(sum>n)break; } cout<<flag<<endl; } return 1; } 我打了表,再枚举就OK了,希望高手指点 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator