POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 19895 | Accepted: 10906 |
Description
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
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
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
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
题目分析:给你一个数,如果这个数能够表示成几个连续的素数相加的形式,请问有多少种这样的形式。
例如:41=2+3+5+7+11+13
41=11+13+17
41=41 共有3种
像这种7+13=20 或者 3+5+5+7=20 这些都是不合法的。
算法分析:数据范围不大,先将1000以内的素数存在一个数组里,两层循环进行暴力枚举
外层循环控制最小的素数累加和的起点数,从它开始一直累加下去直到>=输入数。
如果累加和==n,计数器++,最后输出结果。
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; int prime[2000], total=0; bool isprime(int k )
{
for(int i=0; i<total; i++)
{
if(k%prime[i] == 0)
{
return false;
}
}
return true;
} int main()
{
int i, j;
for( i=2; i<=10000; i++)
{
if(isprime(i))
{
prime[total++]=i;
}
}
prime[total]=10001; //??? int n;
while(scanf("%d", &n)!=EOF)
{
if(n==0)
break;
int ans=0;
for(i=0; n>=prime[i]; i++)
{
int cnt=0;
for(j=i; j<total &&cnt<n; j++)
{
cnt+=prime[j];
}
if(cnt==n)
{
++ans;
}
}
printf("%d\n", ans );
}
return 0;
}
POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )的更多相关文章
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
随机推荐
- python模块(二)
一.json模块 作用: 用于[字符串]和 [python基本数据类型] 间进行转换 Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding. encoding ...
- python结构语句(while,if)
一.基础语法 编码: 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串 #!/usr/bin/env python # -*- coding:utf- ...
- idea修改变量及其引用
idea 修改某一变量及其引用 选中变量 shift+f6(shift+fn+f6), ctrl+R的当前页面全局替换, ctrl+shift+R 项目中的全局替换
- Python3:urllib模块的使用
Python3:urllib模块的使用1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=N ...
- DDCTF2019逆向分析前俩题WriteUP
DDCTF2019 笔者做了前俩道题.冷不丁过去一个月了.现在在此做一下WriteUp:题目链接: 1:题目1 2:题目2 reverse1:writeup: 1.程序打开后如下所示 2.查壳结果为U ...
- Java截取视频首帧并旋转正向
package test; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import j ...
- MD5进行文件完整性校验的操作方法
我组产品包含大量音频和图片资源,MD5主要就用来检测这些资源文件的完整性.主要思路是:先计算出所有资源文件的MD5值,存到一个xml文件中,作为标准的MD5值.然后把这个xml文件放到我们的产品中,每 ...
- ios下读取jason中的nsstring时间并本地化成中文gmt时间显示上午下午
https://developer.apple.com/library/ios/qa/qa1480/_index.html - (NSDate *)dateFromString:(NSString * ...
- iOS Block学习
iOS4已经直接支持blocks,很有必要学习一下. 在ios中,将blocks当成对象来处理,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其 本身又 ...
- 常用Git命令手册
常用Git命令手册 此文只是对Git有一定基础的人当记忆使用,比较简略,初级学员强烈推荐廖雪峰老师的Git系列教程,通俗易懂,戳此处即可开始学习 1.安装Git Linux sudo apt-get ...