POJ 1595 Prime Cuts(我的水题之路——素数取中间区域,准确定位)

Prime Cuts
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7774   Accepted: 2978

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,求在1到N之间的素数中,如果其中素数总是是奇数,则输出中间的2*C-1,如果其中素数总数是偶数,则输出中间的是2*C个素数

解法是打表+查找,先用筛法,将prime数组中素数标记出来,同时标记出,这个是第几个素数,将所有的素数存到primes数组中。然后读入N和C,利用prime数组,找到小于等于N的最大素数是几,这个数是第几个素数,存在num变量里,之后,用at,to两个变量定位输出起始点和终止点,情况如下:
1)如果num是奇数,则
            at = num / 2 - C + 1;
            to = at + 2 * C - 1;
2)如果num是偶数,则
            at = num / 2 - C;
            to = at + 2 * C;
3)如果num小于要输出的个数,则将N以下所有的素数输出。

注意点:
1)每个输出结果后面有个单独空行(1PE)
2)at和to的定位要准确。

代码(1AC):
#include <cstdio>
#include <cstring>
#include <cstdlib>

#define N 1000
#define M N + 100

int prime[M];
int primes[N];

void init(){
    int i, j, k;

    memset(prime, 0, sizeof(prime));
    memset(primes, 0, sizeof(primes));
    for (i = 0; i <= N; i++){
        prime[i] = 1;
    }
    prime[0] = 0;
    prime[1] = 1;
    for (i = 2; i <= N / 2; i++){
        if (prime[i] == 1){
            for (j = i * 2; j <= N; j += i){
                prime[j] = 0;
            }
        }
    }
    for (i = j = 0, k = 1; i <= N; i++){
        if (prime[i] != 0){
            prime[i] = k++;
            primes[j++] = i;
        }
    }
}

int main(void){
    int MAX, C;
    int i, j;
    int at, to;
    int num;

    init();
    while (scanf("%d%d", &MAX, &C) != EOF){
        printf("%d %d:", MAX, C);
        to = MAX;
        while (prime[to] == 0){
            to --;
        }
        num = prime[to];
        //printf("to: %d\nnum: %d\n", to, num);
        if (num % 2 == 0 && 2 * C >= num || num % 2 == 1 && 2 * C - 1 >= num){
            for (i = 0; i < num; i++){
                printf(" %d", primes[i]);
            }
            printf("\n\n");
            continue;
        }
        if (num % 2 == 1){
            at = num / 2 - C + 1;
            to = at + 2 * C - 1;
        }
        else if (num % 2 == 0){
            at = num / 2 - C;
            to = at + 2 * C;
        }
        to -= 1;
        for (i = at; i <= to; i++){
            printf(" %d", primes[i]);
        }
        printf("\n\n");
    }
    return 0;
}


你可能感兴趣的:(c,list,input,each,output,Numbers)