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. JavaScript Unicode字符操作

    charCodeAt() 方法 定义和用法charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数.方法 charCodeAt() 与 ...

  2. (转)php中__autoload()方法详解

    转之--http://www.php100.com/html/php/lei/2013/0905/5267.html PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例 ...

  3. 用ASP.net判断上传文件类型的三种方法

    一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false;           ...

  4. ViewHolder的作用和用法

    一直都看别人用ViewHolder,自己也用过,却不知道它的作用是什么?但知道肯定很有用,而且现在android studio应该有直接生产Viewholder的插件, 不过博主我是个新手,就没尝试去 ...

  5. Directory类

    string[] pathes = Directory.GetDirectories(@"G:\ReawFiles");//获得所有的文件夹 string[] pathes = D ...

  6. sql 列轉行、行轉列

    PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...

  7. RAC检查各资源

  8. 想加入一行代码吗?使用<code>标签

    在介绍语言技术的网站中,避免不了在网页中显示一些计算机专业的编程代码,当代码为一行代码时,你就可以使用<code>标签了,如下面例子: <code>var i=i+300;&l ...

  9. SVN管理工具Cornerstone之:创建分支、提交合并

      创建工程的分支: 步骤: 1.选择左下角仓库repositories中的工程名->选择trunk->点击Branch->在提示框里填写分支名称create, 2.在做上角work ...

  10. makefile简单介绍

    现在的IDE环境大多是高度集成的,只需要按一个按钮即可完成编译-汇编-链接的工作,但是实际在嵌入式开发的过程中,需要根据实际需要编写个性化的需求,这就需要掌握makefile的写法. 高级IDE的方便 ...