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 |
开1229大的数组,附代码// Sum of Consecutive Prime Numbers.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<iomanip> #include<cmath> #include<fstream> using namespace std; int main(int argc, char* argv[]) { ifstream cin("a.txt"); int n,summ; int prim[1229]; int i,j,sum=0; double m; //将1-10000之间的素数保存到prim数组中 for(m=2;m<=10000;m++) { for(j=2;j<=sqrt(m);j++) if((int)m%j==0) goto here; prim[sum++]=m; here:; } /* cout<<"sum="<<sum<<endl; for(i=0;i<sum;i++) printf("%10d",prim[i]); */ while(cin>>n) { if(n==0) break; summ=0; for(i=0;i<sum;i++) { int su=0; for(j=i;;j++) { su+=prim[j]; if(su>n) break; if(su==n) { // for(int k=i;k<=j;k++) // cout<<setw(7)<<prim[k]; // cout<<endl; summ++; } } } /* for(i=2;i<sqrt(n);i++) if(n%i==0) {break;summ--;} summ++; */ printf("%d\n",summ); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator