首页 > ACM题库 > HDU-杭电 > HDU 1319 Prime Cuts-数论-[解题报告] C++
2013
12-09

HDU 1319 Prime Cuts-数论-[解题报告] C++

Prime Cuts

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

21 2
18 2
18 18
100 7

样例输出:

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

点击打开链接

分析:

给n和c,如果n为偶数,输出1-n中间附近的(2*c)-1个素数,反之输出2*c个。。。

输出时没输出n和c,WA了一次。。

没出空行PE了一次。。

好困啊,想睡觉了。。。

#include"stdio.h"
#include"string.h"
#include"math.h"
int prime[331],cnt;
void fun()
{
    int a[1001];
    memset(a,0,sizeof(a));
    int i,j;
    for(i=2;i<=1000;i++)
    {
        if(a[i]==0)
        {
            for(j=i+i;j<=1000;j+=i)
                a[j]=1;
        }
    }
    cnt=0;
    for(i=1;i<=1000;i++)
    {
        if(a[i]==0)prime[cnt++]=i;
    }
    //printf("%d",cnt);
}
int main()
{
    int n,c;
    int a,b;
    int i,j,t;
    fun();
    while(scanf("%d%d",&n,&c)!=-1)
    {
        int t;
        t=0;
		printf("%d %d: ",n,c);
        for(i=0;i<cnt;i++)
        {
            if(prime[i]>n)break;
        }
        if(c>=i)
        {
            for(j=0;j<i-1;j++)
                printf("%d ",prime[j]);
            printf("%d\n",prime[j]);
        }
        else
        {
            t=i;
            if(t%2==1)
            {
                a=t/2-c+1;
                b=t/2+c-1;
            }
            else
            {
                a=t/2-c;
                b=t/2+c-1;
            }
            for(i=a;i<b;i++)
                printf("%d ",prime[i]);
            printf("%d\n",prime[i]);
        }
		printf("\n");
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/yangyafeiac/article/details/8911542


  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3