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 ...
随机推荐
- Android视频直播解决方案(rstp、udp)
做局域网视频直播有两种方案,通过rstp或udp协议. 1.rstp协议,网络上有个开源项目,基于Android,且这个项目也是一个服务端,里面也集成了http访问页面,可以通过http或者rstp直 ...
- 北航 编译实践 PL/0文法
编译实践-PL\0编译系统实现 姓名: 专业: 计算机科学与技术 学院: 软件学院 提交时间: 2013年12月25日 北京航空航天大学·软件学院 编译实践-PL\0编译系统实现 实验要求 以个人 ...
- Activity启动模式 及 Intent Flags 与 栈 的关联分析
http://blog.csdn.net/vipzjyno1/article/details/25463457 Android启动模式Flags栈Task 目录(?)[+] 什么是栈 栈 ...
- POJ1860Currency Exchange(Bellman + 正权回路)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23938 Accepted: 867 ...
- [Angularjs]国际化
写在前面 在项目中,有用到国际化,跟着就了解了下使用angularjs实现的国际化,这里做一下记录. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]n ...
- Circular Sequence,ACM/ICPC Seoul 2004,UVa 1584
#include <stdio.h> #include <string.h> #define maxn 105 int lss(const char *s,int p,int ...
- LWP 轻量级线程的意义与实现
转子 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 二.Linux 2.4内核中的轻量进程实现 最初的进程定义都包含程序.资源 ...
- Javascript检测用户注册信息
<html> <head> <title>用户注册</title> <meta http-equiv="content-type&quo ...
- 通过ajax访问aspx的CodeBehind中的方法
引言 在项目中突然看到,aspx中的ajax可以访问aspx.cs中的方法,觉得很新奇,也许是lz少见多怪,不过,真的有发现新大陆似的那种兴奋,你也许知道这代表什么,学会了这种方式,代表你以后,可以建 ...
- 老码农教你在 StackOverflow 上谈笑风生
作为一个高大上的码农,你肯定用到过 StackOverflow,必须的.会有人否定这个断言么?那他恐怕不是真正的码农,或者说还没入门.StackOverflow 对于码农的重要性,基本就和诸葛亮对刘备 ...