Prime Cuts

Prime Cuts

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 29
Problem 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

 

 

Source
South Central USA 1996
题目大意:
有多组测试样例,每一行输入两个数字,N,C,N表示在1~N中,需要求出所的素数。C,表示,在你找到的所有的素数(假如有m个素数)中,从中间分开,如果m是奇数X=M/2+1,输出(X-C+1,X+C-1)的所有素数。如果,m是偶数X=M/2,输出(X-C+1,X+C)的所有素数。注意判断下边界十分越界就行了、
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define Max(a,b) a>b? a:b
 4 #define Min(a,b) a>b? b:a
 5 
 6 int main()
 7 {
 8     int i,j,a[1105]={0},sign[1105],k,N,C;
 9     for(i=1;i<=1105;i++)            /*素数筛选*/
10         a[i]=1;
11     for(i=2;i<=1105;i++)
12         if(a[i])
13             for(j=i+i;j<=1105;j+=i)
14                 a[j]=0;
15     while(scanf("%d%d",&N,&C)!=EOF)
16     {
17         memset(sign,0,sizeof(sign));    /*初始化sign[],用来依次存储素数的值*/
18         for(i=1,k=1;i<=N;i++)
19             if(a[i])
20                 sign[k++]=i;
21         i=Max(k/2-C+1,1);               /*获取左区间的最大值*/
22         j=Min(k/2+C-1,k-1);             /*获取右区间的最小值*/
23         printf("%d %d:",N,C);           
24         if(k%2!=0&&j+1<k)j+=1;          /*输出(i,j)范围内的素数,既是题目所求的答案*/
25             for(;i<=j;i++)
26                 printf(" %d",sign[i]);  
27         printf("\n\n");                 /*注意题目所要求的输出格式*/
28     }
29     return 0;
30 }
View Code

 修改:2015.6.1(效率会比较高)

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 int Pr[1010]={0};/*素数表*/
 6 int PR[1010];/*记录所有素数*/
 7 int Count[1010];/*Count[i]记录1~i有多少个素数*/
 8 void Cread()
 9 {
10     int i,j;
11     for(i=1;i<=1000;i++)Pr[i]=1;
12     for(i=2;i<=1000;i++)
13     {
14         if(Pr[i])
15             for(j=2;i*j<=1000;j++)Pr[i*j]=0;
16     }
17     for(i=1,j=0;i<=1000;i++)
18     {
19         if(Pr[i])PR[j++]=i;
20         Count[i]=j;/*记录素数的个数*/
21     }
22     return ;
23 }
24 int main()
25 {
26     int N,C,X,L,R,i;
27     Cread();
28     while(scanf("%d%d",&N,&C)!=EOF)
29     {
30         X=Count[N];/*获取素数的个数*/
31         L=1;R=Count[N];
32         if(X%2)/*判断素数个数的奇偶*/
33         {
34             X=X/2+1;
35             if(X-C+1>L)L=X-C+1;/*注意边界*/
36             if(X+C-1<R)R=X+C-1;/*注意边界*/
37         }
38         else
39         {
40             X=X/2;
41             if(X-C+1>L)L=X-C+1;/*注意边界*/
42             if(X+C-1<R)R=X+C;/*注意边界*/
43         }
44         printf("%d %d:",N,C);
45         for(i=L;i<=R;i++)
46             printf(" %d",PR[i-1]);putchar(10);putchar(10);
47     }
48     return 0;
49 }
View Code

 

posted @ 2014-08-22 13:04  Wurq  阅读(220)  评论(0编辑  收藏  举报