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 ...
随机推荐
- 【HDU 1009】FatMouse' Trade
题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...
- 【poj1745】 Divisibility
http://poj.org/problem?id=1745 (题目链接) 题意 给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k. Solution 水题一 ...
- POJ3714 Raid
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10625 Accepted: 3192 Description ...
- Android 设计模式 之 单例模式
http://blog.csdn.net/fangchongbory/article/details/7734199 目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ...
- 自动打包iOS项目
基于Lexrus的博文iOS-makefile,本文对自动打包涉及到的操作步骤以及理论基础进行了适当的补充. 请在阅读本文前先阅读<iOS makefile>.文章地址:http: ...
- 栈的的顺序实例SeqStack实现
1.#include <stdio.h>#include <stdlib.h>#include "SeqStack.h"/* run this progra ...
- java 内存分析
Java堆内存(heap memory)的十个要点: 1. Java堆内存是操作系统分配给JVM的内存的一部分. 2. 当我们创建对象时,它们存储在Java堆内存中. 3. 为了便于垃圾回收,Java ...
- JDBCTemplate基础学习
JDBCTemplate:spring提供的用于操作数据库的模板,类似DbUtils.使用时必须设置数据源(DataSource):数据源如DBCP.C3P0等 一.JDBCAPI简单使用Demo 1 ...
- SQL防注入程序 v1.0
/// ***************C#版SQL防注入程序 v1.0************ /// *使用方法: /// 一.整站防注入(推荐) /// 在Global.asax.cs中查找App ...
- msmms (二) sms与mms 简述!
mms 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . MMS是英文缩写,它可以是Membership Management System的缩写,中文译名为会员管理系统.也可以是M ...