zoukankan      html  css  js  c++  java
  • poj 1595 Prime Cuts

    Prime Cuts
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10610   Accepted: 4046

    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

     

    题意:给定你一个数n,让你求出1-n内有多少个素数,再给你一个数d,如果2*d大于素数的个数则全部输出;

    否则,如果个数为单数,输出2*d - 1个并且以中间那个素数为中心分别向两边输出d-1个;

    如果偶数个,输出2*d个,也是以中间那个素数为中心向两边扩展;————网上查的

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 #define max 1000
     6 int prime[max+10];
     7 void get_prime(int n){
     8     int i;
     9     prime[0]=1;
    10     prime[1]=2;
    11     int index=1;
    12     for(i=3;prime[index]<=n;i++){//得到第一个大于n的素数 
    13         int j=1;//j不能==0!! 
    14         for(;j<=index&&prime[j]*prime[j]<=i;j++){
    15             if(!(i%prime[j])){
    16                 break;
    17             }
    18         }
    19         if(prime[j]*prime[j]>i){
    20             prime[++index]=i;
    21         }
    22     }
    23     //cout<<prime[index]<<endl;
    24 }
    25 int main()
    26 {
    27     get_prime(max);
    28     int n,c;
    29     while(cin>>n>>c){
    30         int i=0,j,s,e;
    31         while(prime[i]<=n){
    32             i++;
    33         }
    34         if(i%2){
    35             if(i<2*c-1){
    36                 s=0;
    37                 e=i-1;
    38             }
    39             else{
    40                 s=i/2-(c-1);
    41                 e=i/2+(c-1);
    42             }
    43         }
    44         else{
    45             if(i<2*c){
    46                 s=0;
    47                 e=i-1;
    48             }
    49             else{
    50                 s=i/2-1-(c-1);
    51                 e=i/2+(c-1);
    52             }
    53         }
    54         cout<<n<<" "<<c<<":";
    55         for(j=s;j<=e;j++){
    56             cout<<" "<<prime[j];
    57         }
    58         cout<<endl;
    59         cout<<endl;//格式注意
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    Save a bricked Samsung Note 3 and do extraction
    Use EnCase to acquire data from a smartphone
    Android手机与计算机间的”信任关系”
    iPhone 6 (iOS 9.2) extractiion failed by XRY
    No deleted LINE chat messages recovered on iOS 9.1 after UFED extraction
    Find out who the “mole” is?
    Recover deleted pictures in iOS 9
    工厂模式
    教练和运动员案例
    本地网络共享
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4297860.html
Copyright © 2011-2022 走看看