poj Sum of Consecutive Prime Numbers 模拟
2016-09-11 13:01
435 查看
题目大意:输入数的范围在2到10000,要求求出输入数可以由小于等于它的素数之和,且这些素数要求相邻。如41有:2+3+5+7+11+13, 11+13+17, 41. 只有3种
#include<iostream> using namespace std; const int maxn = 20000; bool a[maxn]; int b[maxn]; //错误将存储答案的数组声明为bool型的 int len; void isprime(int *ans,int &len)//参数引用传递 { for(int i=1;i<maxn;i++) a[i] = true; a[1] = false; for(int i=2;i<maxn;i++) { if(a[i]==true) for(int j = i*i;j<maxn;j+=i) a[j]=false; } int k=0; for(int i=1;i<maxn;i++) { if(a[i]) { ans[k]=i; k++; } } len = k; } int main() { int m; while(cin>>m&&m) { int n=0; int i=1,sum=0,j=0; isprime(b,len); for(i=0;i<len;i++) { sum+=b[i]; //cout<<b[i]<<" "; if(sum==m) n++; if(sum>m)//从头开始减 { for(j;j<len;j++) { //cout<<"-"<<b[j]<<" "; sum-=b[j]; if(sum==m) n++; if(sum<m) { j++; break;//跳出循环时将游标移动至下一位 } } } if(b[i]>m)break; } cout<<n<<endl; } }
相关文章推荐
- POJ 2739 Sum of Consecutive Prime Numbers 数论
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ Sum of Consecutive Prime Numbers
- poj Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ2739 - Sum of Consecutive Prime Numbers(素数问题)
- poj - 2739 - Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- 【原】 POJ 2739 Sum of Consecutive Prime Numbers 筛素数+积累数组 解题报告
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ-2739-Sum of Consecutive Prime Numbers
- 【POJ-2739】Sum of Consecutive Prime Numbers