poj 2739 Sum of Consecutive Prime Numbers 解题报告
题目链接: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 解题报告的更多相关文章
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- 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
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【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- 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( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
随机推荐
- 列表 list
例子: #!/usr/bin/python li = list([11,22,33,66,99,77]) print(li) li.extend((44,55,)) print(li) ret = l ...
- UVa 1347 Tour
Tour Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Description Joh ...
- 洛谷P2024 食物链
挺神奇 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种 ...
- 洛谷P2731骑马修栅栏
题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个 ...
- easyui form 方式提交数据
http://ldzyz007.iteye.com/blog/2067540 <form id="ff" method="post"> . ...
- php闭包支持
匿名函数提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() { }; //带结束符 可以看到 ...
- java常见异常类图(分类了Error/RuntimeExecption、check Exception)
版权:欧初权 http://www.cnblogs.com/langtianya/p/4435537.html
- 漫长Appium之路(二)——Appium安装与使用总结
前面介绍了iOS自动化工具的Appium所需的虚拟机环境,接下来介绍下Appium的安装与使用方法,这个足足折腾我将近一个星期.网上没有什么详细的资料,对于遇到的各种各样问题也没用提供明确的解决方法. ...
- C#面向对象面试题集锦
1.简述C#中的虚方法 答:注意:当使用virtual关键字修饰符后,不允许再同时使用abstract,static,或override关键字进行修饰 使用virtual关键字修饰的方法就是虚方法,虚 ...
- 键盘上各键对应的ASCII码与扫描码
键盘上各键对应的ASCII码与扫描码 vbKeyLButton 0x1 鼠标左键vbKeyRButton 0x2 鼠标右键vbKeyCancel 0x3 CANCEL 键vbKeyMButton 0x ...