POJ1595_Prime Cuts【素数】【水题】
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【素数】【水题】的更多相关文章
- zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)
1775: 和尚特烦恼1--是不是素数 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 563 Solved: 193 SubmitStatusWeb ...
- hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题
题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...
- 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)
算法训练 Torry的困惑(基本型) 时间限制:1.0s 内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)
毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一 ...
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- 一道cf水题再加两道紫薯题的感悟
. 遇到一个很大的数除以另一个数时,可以尝试把这个很大的数进行,素数因子分解. . 遇到多个数的乘积与另一个数的除法时,求是否能整除,可以先求每一个数与分母的最大公约数,最后若分母数字为1,则证明可整 ...
- HDU 2521 反素数 模拟题
解题报告:水题,直接附上代码,只是觉得这题的作者是不是吃饱了饭撑的,反素数的概念跟这题一点关系都没有. #include<cstdio> int judge1(int k) { ; ;i& ...
- Goldbach`s Conjecture(素筛水题)题解
Goldbach`s Conjecture Goldbach's conjecture is one of the oldest unsolved problems in number theory ...
随机推荐
- 表达式:使用API创建表达式树(1)
表达式树可使用Expressions类的静态工厂方法来创建.这种用API的方式创建给予我们在编程极大的灵活性,MSDN上关于表达式的例子也不少,但在使用过程中还是会遇到许多麻烦,对有的表达式类,介绍得 ...
- 标量类型(scalar)
(ISO C11 §6.2.5) Arithmetic types and pointer types are collectively called scalar types. Array and ...
- (转帖) 有限狀態機FSM coding style整理 (SOC) (Verilog)
来源:http://www.codesoso.net/Record/101092_95120_21.html 来源:http://www.cnblogs.com/oomusou/archive/201 ...
- STL容器介绍
STL的容器可以分为以下几个大类: 一:序列容器, 有vector, list, deque, string. 二 : 关联容器, 有set, multiset, map, mulmap, h ...
- javascript写的ajax请求
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- 说一说&&符
今天添加检测代码,还得添加好几套,好蛋疼. 为了省点事,全写在HTML的行间onclick里,为此专门琢磨了一下&&的用法. 一般用&&(除了逻辑判断里),是为了简写i ...
- zend studio设置
1.字体设置: 第一步:进入设置窗口 windows -> preferences 第二步:进入修改字体的选项卡. General -> Appearance -> Co ...
- easyui tabs 真正刷新用法
//刷新当前标签Tabs function RefreshTab(currentTab) { var url = $(currentTab.panel('options')).attr('href') ...
- Linux的时钟管理
本文转自博客园zhenwenxian的Linux时间管理,很详细,写得很不错,对初学者还是有很大帮助的. 时间管理在内核中占有非常重要的地位.相对于事件驱动,内核中有大量的函数都是基于时间驱动的.内核 ...
- 百度统计js被劫持用来DDOS Github的JS注释
前几天在乌云看见了百度统计js被劫持用来DDOS Github,就想看看执行的核心JS是怎么样请求的. 就分析了下JS的执行,发现乌云解析的地方说错了. 文章里面说.大概功能就是关闭缓存后每隔2秒加载 ...