素数 UVA 406

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也要有输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值