poj 2739
来源:互联网 发布:跑跑卡丁车淘宝好便宜 编辑:程序博客网 时间:2024/06/05 14:53
http://poj.org/problem?id=2739
Description
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.
Output
The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.
Sample Input
2317412066612530
Sample Output
11230012
两种方法。。。
我写的:
#include <iostream>#include <string.h>#include <stdio.h>using namespace std;int p[10000],prime[10000],a[10000][10000];int main(){ int k=0; memset(prime,true,sizeof(prime)); for(int i=2; i<=10000; i++) { if(prime[i]) { p[k++]=i; for(int j=i*i; j<=10000; j+=i) prime[j]=false; } } /*for(int i=0;i<100;i++) cout << p[i] << " ";*/ for(int i=0;i<k;i++) for(int j=i;j<k;j++) { if(j==0) a[i][j]=p[j]; else a[i][j]=p[j]+a[i][j-1]; } /*for(int i=0;i<10;i++) { for(int j=0;j<10;j++) cout << a[i][j]<<" "; cout << endl; }*/ int p; while(cin >> p) { if(p==0) break; int sum=0; for(int i=0;i<k;i++) for(int j=0;j<k;j++) if(a[i][j]==p) sum++; printf("%d\n",sum); } return 0;}
队友写的:
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#define deb system("pause")int p[1000];const int maxn=10000;int predeal(){ bool isp[maxn+5]; memset(isp,true,sizeof(isp)); int n=0,j; for(int i=2;i<maxn+2;i++) { if(isp[i]) { p[n++]=i; for(j=i*i;j<maxn+2;j+=i) isp[j]=false; } } return n;}int main(){ int n=predeal(); int a,ans,sum; while(scanf("%d",&a)&&a) { ans=0; for(int i=0;i<n-1;i++) { sum=0; for(int j=i;j<n;j++) { sum+=p[j]; if(sum>=a) { if(sum==a)ans++; break; } } } printf("%d\n",ans); } return 0;}
0 0
- POJ 2739
- poj 2739
- poj 2739
- POJ 2739
- POJ 2739
- poj-2739
- POJ 2739
- poj 2739
- poj 2739
- POJ 2739
- POJ 2739
- poj 2739
- Poj 2739
- POJ 2739
- POJ 2739
- POJ 2739
- poj 2739
- 【POJ】2739
- C#中简单的绘图
- 英语宾语从句 引导词的选择
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(5)--理解数据绑定的基础
- Thrift python服务端出错 解决方法
- Apache Hadoop 2.3.0 Released!
- poj 2739
- 第四周作业
- gcc的__attribute__编译属性
- POJ 2531
- 好想写点儿什么,但是不知道怎么写
- 2_猜数字游戏_140329(C开发)
- iPhone Http 服务器Mongoose的使用
- c代码优化
- 【LeetCode】Interleaving String && CSDN 交替字符串