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 ...
随机推荐
- 修改mysql最大连接数的方法
MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个 方法一:进入MYSQL安装目录 打开MYSQL配置文件 ...
- 蝙蝠算法-python实现
BAIndividual.py import numpy as np import ObjFunction class BAIndividual: ''' individual of bat algo ...
- BZOJ-1925 地精部落 烧脑DP+滚动数组
1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...
- 【bzoj3150】 cqoi2013—新Nim游戏
www.lydsy.com/JudgeOnline/problem.php?id=3105 (题目链接) 题意 在第一个回合中,第一个游戏者可以直接拿走若干个整堆的火柴.可以一堆都不拿,但不可以全部拿 ...
- maven加载spring包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artif ...
- Matlab中的括号()[]{}
Matlab中的括号()[]{} Matlab中经常会用到括号去引用某Array或者是cell的内容,但三者有什么具体区别呢?[ ] 中括号用来构建向量(Vectors)或者是矩阵(Matrices) ...
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- [LeetCode] next_permutation
概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用 ...
- git小结
1.创建本地与远程分支 先创建远程分支,再创建本地分支,再将本地分支与远程分支关联git fetch 获取远程分支git checkout remote_branch 或者 git checkout ...
- hibernate的pojo和xml文件