素數 UVA 406

十五古人發表於2019-03-11

Prime Cuts

大致題意

從1-1000裡找出所有素數。1-N裡有多少個素數(包括1與N),C是1-N內的素數中從中間那一個到兩邊類似“半徑”。具體看題意第一段

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 2C prime numbers from the center of the list if there are an even number of prime numbers or 2C − 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 2C prime numbers to be printed from the center of the list if the length of
the list is even; or the 2C − 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

#include <stdio.h>
#include <stdlib.h>
#include"string.h"
#include"math.h"
int findprimary(int a[]);
int main()
{
    int  a[1100]= {1,2,3},i,indexmax,c,limit,limitpos,cposL,cposR,mpos;
    indexmax=findprimary(a);
    while(scanf("%d %d",&limit,&c)==2)
    {

        if(limit>=997){limitpos=indexmax;}//N<=1000,而最大素數為997
        else
        {
            for(i=0; i<=indexmax; i++)
            {
                if(a[i]>limit)
                {
                    limitpos=i-1;
                    break;
                }
            }
        }


        if((limitpos+1) % 2 == 1)
        {
            if( (2*c-1) >=(limitpos+1))
            {
                printf("%d %d:",limit,c);
                for(i=0; i<=limitpos; i++)
                {
                    printf(" %d",a[i]);
                }
            }
            else
            {
                mpos=limitpos/2;
                cposL=mpos-(c-1);
                cposR=mpos+(c-1);
                printf("%d %d:",limit,c);
                for(i=cposL; i<=cposR; i++)
                {
                    printf(" %d",a[i]);
                }
            }
        }

        if((limitpos+1) % 2 == 0)
        {
            if( (2*c) >=(limitpos+1))
            {
                printf("%d %d:",limit,c);
                for(i=0; i<=limitpos; i++)
                {
                    printf(" %d",a[i]);
                }
            }
            else
            {
                mpos=limitpos/2;
                cposL=mpos-(c-1);
                cposR=mpos+1+(c-1);
                printf("%d %d:",limit,c);
                for(i=cposL; i<=cposR; i++)
                {
                    printf(" %d",a[i]);
                }
            }
        }
        printf("\n\n");
    }
    return 0;
}

int findprimary(int a[])
{
        int i,t,j,k=3;
        for(i=5; i<=1000; i=i+2)
        {
            t=sqrt(i);
            for(j=3; j<=t; j++)
            {
                if(i % j ==0)
                {
                    break;
                }
            }
            if(j==t+1)
            {
                a[k++]=i;
            }
        }
        return k-1;
}

注意:1000內的素數最大為997,也就是確保998 999 1000也要有輸出。