POJ1595 Prime Cuts

Prime Cuts
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11961   Accepted: 4553

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

[Submit]   [Go Back]   [Status]   [Discuss]

这个题很迷。。出错几乎都是格式错误。WA,PE了好多发。。

反正以后格式问题不乱搞就行了。

当做警戒

 

 1 #include <cstdlib>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include<iostream>
 6 #include <cmath>
 7 #define ll long long 
 8 #define dscan(a) scanf("%d",&a)
 9 #define mem(a,b) memset(a,b,sizeof a)
10 using namespace std;
11 #define MAXL 1105
12 #define maxn 1000005
13 int p[maxn],res,check[maxn];
14 int f[maxn][2],cnt;
15 void getp()
16 {
17     mem(check,0);
18     check[0]=check[1]=1;
19     for (int i = 2; i <=MAXL; ++i)
20     {
21      if (!check[i])
22        {
23          p[cnt++] = i;
24        }
25     for (int j = 0; j < cnt; ++j)
26     {
27        if (i * p[j] > MAXL)
28          {
29            break;
30           }
31     check[i*p[j]] = 1;
32     if (i % p[j] == 0)
33     {
34       break;
35     }
36   }
37 }
38 }
39 int main()
40 {
41       int n,c;
42     getp();
43     check[1]=0;
44     while(cin>>n>>c)
45     {
46       int sum=0;
47       int pp[1105];
48       mem(pp,0);
49       cout<<n<<" "<<c<<":";
50       for(int i=1;i<=n;++i)
51        if(!check[i]) pp[++sum]=i;
52       //cout<<"sum="<<sum<<endl;
53       if((sum%2&&sum<=2*c-1)||(sum%2==0&&sum<=2*c))
54       {
55         for(int i=1;i<=sum;++i) printf(" %d",pp[i]);
56         printf("\n\n");
57       }
58       else
59       {
60         if(sum%2)  {
61           int mid=(sum+1)/2;
62           for(int i=mid-(c-1);i<=mid;++i) printf(" %d",pp[i]);
63           for(int i=mid+1;i<=mid+(c-1);i++) printf(" %d",pp[i]);
64           printf("\n\n");
65            }   
66         else 
67         {
68            for(int i=(sum-2*c)/2+1;i<=(sum-2*c)/2+2*c;++i) printf(" %d",pp[i]);
69            printf("\n\n");
70        }  
71       }
72     }
73     return 0;
74 }
View Code

 

 

 

posted @ 2018-07-27 14:57  Maydaytyh  阅读(239)  评论(0编辑  收藏  举报