poj1595Prime Cuts

来源:互联网 发布:在淘宝上怎样联系客服 编辑:程序博客网 时间:2024/05/29 14:02

题意:
给你一个数,n,c,你要做的就是将1到n之间的素数按照顺序(升序)全部找出来(本题中1也是素数)
1.如果你找出来的素数的总个数(count)是奇数,就输出这一串素数中最中间的2*c-1个素数。如果素数总个数小于等于2*c-1,就将你找到的素数按照升序全部输出;如果素数总个数大于2*c-1,就输出这一串素数中最中间的2*c-1个素数。
(例如:一串素数为1,2,3,5,7,11,13;共7个,假如你需要输出的是3个的话,就得从3开始输出,输出的一串数为:3,7,11.)
2.若你找出来的素数总个数是偶数,就输出这一串素数中最中间的2*c个素数。其它执行与第1种情况相同,就是偶数时执行的是2*c。
Prime Cuts
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 10395Accepted: 3976

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 218 18100 7
Sample Output
21 2: 5 7 1118 2: 3 5 7 1118 18: 1 2 3 5 7 11 13 17100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
#include<stdio.h>#include<string.h>#define M 10000int prime[M];int a[M];int main(){int i,j;memset(prime,0,sizeof(prime));prime[0]=1;prime[1]=prime[2]=0;            //在本题中,1也被看做素数 for(i=2;i*i<=M;i++)           //先进行素数打表 {if(!prime[i]){for(j=2*i;j<=M;j+=i)prime[j]=1;}}int n,c,count,t;memset(a,0,sizeof(a)); while(~scanf("%d%d",&n,&c)){count=0;    for(i=1,j=0;i<=n;i++)         //找出1到n之间素数的总数; {    if(!prime[i])    {    a[j++]=i;           //a[0]=1;     count++;           } }printf("%d %d:",n,c);if(count&1)             //如果count为奇数,执行如下处理语句 {if(count<=2*c-1){for(j=0;j<count;j++)printf(" %d",a[j]);}else{t=(count-(2*c-1))/2;for(j=t;j<t+2*c-1;j++)//因为数组a是从0开使的,所以执行时从t开始即可 printf(" %d",a[j]);}}else                         //count为偶数时执行如下处理语句 {if(count<=2*c){for(j=0;j<count;j++)printf(" %d",a[j]);}else{t=(count-2*c)/2;for(j=t;j<t+2*c;j++)printf(" %d",a[j]);}}printf("\n\n");}return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中国单身的老了怎么办 双眼杯盘比增大,怎么办 出完精子腰疼怎么办 备孕2年没怀孕怎么办 发现怀孕了不想要怎么办 造影后通而不畅怎么办 脑梗病人晚上闹怎么办 两眼视力差距400怎么办 脑血清颗粒吃多怎么办 脸过敏吃了海鲜怎么办 如果qq密码忘了怎么办 qq密码被盗了该怎么办 qq号被别人盗了怎么办 qq不想让别人用怎么办 买了金科的房子怎么办 蟹爪莲叶子耷拉怎么办 金钻的叶子发黄怎么办 金钻叶子发焦黄怎么办 红钻叶子黄了怎么办啊 绿钻叶子黄斑点怎么办 金钻的叶子卷怎么办 金钻叶子有黑斑怎么办 怀孕60天没有胎心胎芽怎么办 单位不给交社保怎么办 公司没给足产假怎么办 小公司不给产假怎么办 机关不给陪产假怎么办 刚人流后又怀孕怎么办 怀孕50天不想要怎么办 生了孩子不想要怎么办 刚怀孕不想要孩子怎么办 怀孕一周不想要孩子怎么办 怀孕了不想要孩子怎么办 怀上二胎后悔了怎么办 50岁怀了二胎怎么办 老公那方面太强怎么办 被私人医院骗了怎么办 我特别烦我妈怎么办 刚怀孕了有炎症怎么办 怀孕了但有炎症怎么办 20岁脸上肉松弛怎么办