题目链接:http://poj.org/problem?id=2739

预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total]。然后依次处理每个测试数据。采用双重循环计算n的表示数:

外循环i :  for (i = 0; x >= prime[i]; i++) 的循环结构枚举所有可能的最小素数prime[i];

内循环:   while (ans < x && j < total)   ans += prime[j++];    计算连续素数的和ans,内循环结束时ans>=x。若ans = n,则连续素数的和的表示数为sum++,继续外循环。外循环结束后得出的sum即为问题的解。

 #include <iostream>
using namespace std; const int Maxn = ; // 设定素数表长
int total, prime[Maxn]; int is_prime(int n) // 判断n是否为素数
{
int i;
for (i = ; i < total; i++)
{
if (!(n % prime[i]))
return ;
}
return ;
} int main()
{
int i, j, x, ans, sum;
total = ;
for (i = ; i <= Maxn; i++) // 预先建立素数表
{
if (is_prime(i))
{
prime[total++] = i;
}
}
while (cin >> x && x)
{
sum = ; // 和初始化为0
for (i = ; x >= prime[i]; i++) // 枚举最小素数
{
j = i;
ans = ;
while (ans < x && j < total)
{
ans += prime[j++]; // 求连续素数的和 }
if (ans == x) // 若和恰等于x,则累计答案数 sum++;
if (is_prime(x)) // 该数是素数时,和也要包括自己 sum++;
}
cout << sum << endl;
}
return ;
}

poj 2739 Sum of Consecutive Prime Numbers 解题报告的更多相关文章

  1. POJ.2739 Sum of Consecutive Prime Numbers(水)

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

  2. POJ 2739 Sum of Consecutive Prime Numbers(素数)

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

  3. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

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

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

  6. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

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

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

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

  8. poj 2739 Sum of Consecutive Prime Numbers 小结

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

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

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

随机推荐

  1. 【BZOJ-2818】Gcd 线性筛

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3347  Solved: 1479[Submit][Status][Discuss ...

  2. 【bzoj1042】 HAOI2008—硬币购物

    http://www.lydsy.com/JudgeOnline/problem.php?id=1042 (题目链接) 题意 共有4种硬币,面值分别为c1,c2,c3,c4.某人去商店买东西,去了to ...

  3. 【poj2079】 Triangle

    http://poj.org/problem?id=2079 (题目链接) 题意 求凸包内最大三角形面积 Solution 旋转卡壳. 只会n²的做法,但是竟然过了.就是枚举每一个点,然后旋转卡壳另外 ...

  4. [NOIP2009] 提高组 洛谷P1071 潜伏者

    题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原 ...

  5. HDU1698 Just a Hook

    Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  6. phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...

  7. Codeforces 650C Table Compression

    传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...

  8. C++处理一个动态规划的问题

    嗯哼,别人问的问题,看的我也头晕,百度了一下动态规划,看了看才想起来该怎么做,今天写了写代码,实现了~ 要求是递归,动态规划,想了想这种方法也是最简单的~ 所谓动态规划:把多阶段过程转化为一系列单阶段 ...

  9. Linux X Window System运行原理和启动过程

    本文主要说明X Window System的基本运行原理,其启动过程,及常见的跨网络运行X Window System. 一) 基本运行原理 X Window System采用C/S结构,但和我们常见 ...

  10. sdk和ndk

    让我先来说说android sdk (Android Software Development Kit, 即Android软件开发工具包)可以说只要你使用java去开发Android这个东西就必须用到 ...