Prime Cuts
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 10464Accepted: 3994

Description

A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list
of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1
prime numbers from the center of the list if there are an odd number of prime numbers in the list.

Input

Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed
from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.

Output

For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as
defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one
blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.

Sample Input

21 2

18 2

18 18

100 7

Sample Output



21 2: 5 7 11



18 2: 3 5 7 11



18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

题目大意:给你两个数N和C,算出1~N(包含N)之间的素数序列,

若素数个数为奇数,则输出素数序列中心的2*C-1个素数。

若素数个数为偶数。则输出素数序列中心的2*C个素数。

输出个数中说若C>素数个数。则输出整个素数序列。

思路:筛法求素数打表,之后求出素数序列的中心位置。推断奇偶并输出

注意:此题中。1被当做了质数(仅仅限本题),数据规模开成1000是不够

的。须要开成1100。应该是測试数据超范围了。

#include<stdio.h>

int Prime[1100],PrimeNum[1100];

int IsPrime()
{
for(int i = 1; i <= 1100; i++)
Prime[i] = 1;
for(int i = 2; i <= 1100; i++)
{
for(int j = i+i; j <= 1100; j+=i)
Prime[j] = 0;
}
int num = 1;
for(int i = 0; i <= 1100; i++)
if(Prime[i])
PrimeNum[num++] = i;
return num;
}
int main()
{
int num = IsPrime();
int N,C,pos;
while(~scanf("%d%d",&N,&C))
{
pos = 0;
for(int i = 1; i < num; i++)
{
if(N >= PrimeNum[i] && N < PrimeNum[i+1])
{
pos = i;
break;
}
}
printf("%d %d:",N,C);
if(C > pos && pos%2==0)
C = pos/2;
else if(C > pos)
C = (pos+1)/2;
if(pos%2 == 1)
{
for(int i = (pos-(C*2-1))/2+1; i <= (pos-(C*2-1))/2+C*2-1; i++)
printf(" %d",PrimeNum[i]);
printf("\n\n");
}
else
{
for(int i = (pos-C*2)/2+1; i <= (pos-C*2)/2+C*2; i++)
printf(" %d",PrimeNum[i]);
printf("\n\n");
} }
return 0;
}

POJ1595_Prime Cuts【素数】【水题】的更多相关文章

  1. zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)

    1775: 和尚特烦恼1--是不是素数 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 563  Solved: 193 SubmitStatusWeb ...

  2. hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题

    题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...

  3. 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  4. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  5. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  6. 2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)

    毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一 ...

  7. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  8. 一道cf水题再加两道紫薯题的感悟

    . 遇到一个很大的数除以另一个数时,可以尝试把这个很大的数进行,素数因子分解. . 遇到多个数的乘积与另一个数的除法时,求是否能整除,可以先求每一个数与分母的最大公约数,最后若分母数字为1,则证明可整 ...

  9. HDU 2521 反素数 模拟题

    解题报告:水题,直接附上代码,只是觉得这题的作者是不是吃饱了饭撑的,反素数的概念跟这题一点关系都没有. #include<cstdio> int judge1(int k) { ; ;i& ...

  10. Goldbach`s Conjecture(素筛水题)题解

    Goldbach`s Conjecture Goldbach's conjecture is one of the oldest unsolved problems in number theory ...

随机推荐

  1. SQL后台分页三种方案和分析

    建立表:CREATE TABLE [TestTable] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [FirstName] [nvarchar] (100) CO ...

  2. 【转】 NSArray copy 问题

    转自:   http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html   好久没写博客了,今天看到同事的代码中用到了 copy 这个 方法,之前也有了解 ...

  3. CocoaPods使用详细说明(转)

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...

  4. JavaScript_ECMA5数组新特性

    var arr = [ 1, 2, 3, 4, 5, 4, 3, 2, 1 ]; 新加位置的方法: indexOf lastIndexOf1.1个参数的时候表示传值 返回索引位置(index从0开始) ...

  5. mysql for linux 数据库的安装过程

    mysql for linux 数据库的安装过程 l  安装版本:mysql-advanced-5.6.12-linux-glibc2.5-x86_64.tar.gz ,此版本是绿色版本,只需要将其解 ...

  6. Qt信号槽连接在有默认形参下的情况思考

    写下这个给自己备忘,比如函数 ) 你在调用端如论是test(3)或者test(),都可以正确调用到这个函数. 但是,如果放到Qt中的信号槽的话,这个还是值得讲一讲的,不然的话,可能会引起相应的误会. ...

  7. RemoteWebDriver使用说明

    1. 本地代码使用RemoteWebDriver启动: public class Testing { public void myTest()throws Exception { WebDriver ...

  8. gchart 插件API

    data: 一个二维数组,参数类型如下:[[, , ], [, , ], [, , ]] size: 图片显示的大小 ( width x height ) 300x200 type: 前面已经说过了 ...

  9. MongoDB-性能优化之索引

    首先看一个实例 >;i<;i++){ db.indexdemo.insert({),"create":new Date});} WriteResult({ }) > ...

  10. Python自动化运维之14、设计模式

    设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...