标签:
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数
用的筛法素数打表
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20020 | Accepted: 10966 |
Description
Input
Output
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int prime[10000],num[10005]; int main() { int i,j,k=0,tmp,n; for(i=0;i<=10000;i++) num[i]=1; num[0]=0; num[1]=0; for(i=2;i<=10000;i++) { if(num[i]) { prime[k++]=i; for(tmp=i*2;tmp<=10000;tmp+=i) num[tmp]=0; } } while(scanf("%d",&n)!=EOF&&n) { int ans=0,sum; for(i=0;i<k;i++) { sum=0; for(j=i;j<k&&sum<n;j++) sum+=prime[j]; if(sum==n) ans++; } printf("%d\n",ans); } }
POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/4262293.html