Sum of Consecutive Prime Numbers(poj2739)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22019 | Accepted: 12051 |
Description
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
Output
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
思路:先筛选0到10000的素数,尺取跑一遍。
1 import java.util.*; 2 import java.lang.*; 3 import java.io.*; 4 5 public class Main { 6 public static void main(final String[] args) { 7 Scanner in = new Scanner(System.in); 8 int n, i, j, k, p, q; 9 int aa[] = new int[20000];10 int bb[] = new int[20000];11 Arrays.fill(aa, 0);12 aa[1] = 1;13 aa[0] = 1;14 int cnt = 0;15 for (i = 2; i <= 100; i++) {16 if (aa[i] == 0) {17 for (j = i; i * j <= 10000; j++) {18 aa[i * j] = 1;19 }20 }21 22 }23 for (i = 0; i <= 10000; i++) {24 if (aa[i] == 0) {25 bb[cnt++] = i;26 }27 }28 int ss = 1;29 while (ss == 1) {30 k = in.nextInt();31 int ans = 0;32 if (k == 0) {33 break;34 } else {35 int sum = 0;36 int ll = 0;37 int rr = 0;38 while (ss == 1) {39 if (rr > cnt) {40 break;41 }42 if (bb[rr] > k) {43 break;44 }45 if (ll > rr) {46 break;47 }48 while (ss == 1) {49 sum += bb[rr];50 if (sum >= k) {51 break;52 } else {53 rr++;54 }55 if(rr>cnt)56 {break;}57 }58 if (sum == k) {59 ans++;60 }61 sum -= bb[ll];62 sum -= bb[rr];63 ll++;64 }65 }66 System.out.println(ans);67 }return ;68 }69 }
Sum of Consecutive Prime Numbers(poj2739)的更多相关文章
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25225 ...
- 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 ...
- Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers http://poj.org/problem?id=2739 Time Limit: 1000MS Memory Limit: 6 ...
- 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(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- poj 2379 Sum of Consecutive Prime Numbers
...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
随机推荐
- printf 的 转义词 -转
\n 换行 \r 回车键 \b 退后一格 \f 换页 \t 水平制表符 \v 垂直制表符 \a 发出鸣响 \? 插入问号 \" 插入双引号 \' ...
- Prometheus_exporter安装与使用
Promethues概述:可以看一下更详细的介绍,以下为转载的博客,原文链接,支持原创,请多多支持!!:闫世成的博客园 Prometheus-node-exporter 1.简介: 内核公开的硬件和操 ...
- C/C++ Qt StandardItemModel 数据模型应用
QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...
- 【leetcode】1217. Minimum Cost to Move Chips to The Same Position
We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...
- OC中的结构体
一.结构体 结构体只能在定义的时候进行初始化 给结构体属性赋值 + 强制转换: 系统并不清楚是数组还是结构体,需要在值前面加上(结构体名称) +定义一个新的结构体,进行直接赋值 + ...
- 软件测试人员必备的linux命令
1 目录与文件操作1.1 ls(初级)使用权限:所有人功能 : 显示指定工作目录下之内容(列出目前工作目录所含之档案及子目录). 参数 : -a 显示所有档案及目录 (ls内定将档案名或目录名称开头为 ...
- vue中vuex的五个属性和基本用法
VueX 是一个专门为 Vue.js 应用设计的状态管理构架,统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data ). Vuex有五个核心概念: state, ge ...
- Initialization of data members
In C++, class variables are initialized in the same order as they appear in the class declaration. C ...
- java输入/输出流的基本知识
通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...
- 粒子群算法-PSO
粒子群优化算法 1. 背景知识 1995年美国社会心理学家Kennedy和电气工程师Eberhart共同提出粒子群优化算法(Particle Swarm Optimization, PSO).PSO算 ...