POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

时间:2022-10-20 22:41:35
Sum of Consecutive Prime Numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19895   Accepted: 10906

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.

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

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( *【素数存表】+暴力枚举 )的更多相关文章

  1. POJ 2739 Sum of Consecutive Prime Numbers&lpar;素数&rpar;

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  2. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度&colon;0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  3. POJ&period;2739 Sum of Consecutive Prime Numbers&lpar;水&rpar;

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  4. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  5. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  6. POJ 2739&period; Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  8. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. Android斗地主棋牌游戏牌桌实现源码下载

    本次给大家分享下Android斗地主棋牌游戏牌桌实现源码下载如下: 为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法. 1.主Activity package com.biso ...

  2. i2c总线,设备,驱动之间的关系

    ------ 总线上先添加好所有具体驱动,i2c.c遍历i2c_boardinfo链表,依次建立i2c_client, 并对每一个i2c_client与所有这个线上的驱动匹配,匹配上,就调用这个驱动的 ...

  3. java&period;logging的重定向?

    接着昨天的工作. 上面说要重定向java.util.logging.Logger的输出, 发现也不是不可能. package jmx; import java.util.logging.FileHan ...

  4. sourceInsight使用技巧,持续更新中~~~

    作为测试人员,读各种平台的工程代码时,根本不想安装各种vs或者eclipse等,于是,就找了一款代码阅读工具. sourceInsight,下载地址为官网:http://www.sourceinsig ...

  5. gulpfile的结构

    使用了      yargs     用于获取启动参数,针对不同参数,切换任务执行过程时需要,本项目中的useCache和useSess      path     不明,貌似是用来将某个目录中的文件 ...

  6. Debian耳机声音问题

    Debian耳机声音问题 关于Debian声音问题 debian testing基本系统安装无声音问题解决 耳机声音问题 之前耳机声音一直正常,使用部分软件更改声卡设置后,计算机可以播放外音,但是耳机 ...

  7. 正则-匹配获取url参数

    1.根据指定参数名获取参数值 A页面向连接到B页面的url为: http://www.189dg.com/ajax/sms_query.ashx?action=smsdetail&sid=22 ...

  8. ZOJ3605-Find the Marble&lpar;可能性DP&rpar;

    Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. ...

  9. JPA 不生成外键

    在用jpa这种orm框架时,有时我们实体对象存在关联关系,但实际的业务场景可能不需要用jpa来控制数据库创建数据表之间的关联约束,这时我们就需要消除掉数据库表与表之间的外键关联.但jpa在处理建立外键 ...

  10. &period;NET实现字符串base64编码

    using System; using System.Text; using System.IO; using System.Security; using System.Security.Crypt ...