首页 > 代码库 > HDU 5038 Grade(数学)

HDU 5038 Grade(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5038


Problem Description
Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushrooms and ask him to grade each mushroom according to its weight. Suppose the weight of a mushroom is w, then it’s grade s is 

s = 10000 - (100 - w)^2

What’s more, Ted also has to report the mode of the grade of these mushrooms. The mode is the value that appears most often. Mode may not be unique. If not all the value are the same but the frequencies of them are the same, there is no mode.
 
Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

The first line of each test cases contains one integers N (1<=N<=10^6),denoting the number of the mushroom.

The second line contains N integers, denoting the weight of each mushroom. The weight is greater than 0, and less than 200.
 
Output
For each test case, output 2 lines.

The first line contains "Case #x:", where x is the case number (starting from 1) 

The second line contains the mode of the grade of the given mushrooms. If there exists multiple modes, output them in ascending order. If there exists no mode, output “Bad Mushroom”.
 
Sample Input
3 6 100 100 100 99 98 101 6 100 100 100 99 99 101 6 100 100 98 99 99 97
 
Sample Output
Case #1: 10000 Case #2: Bad Mushroom Case #3: 9999 10000
 
Source
2014 ACM/ICPC Asia Regional Beijing Online

题意:
给出一些蘑菇,由它的重量求出价值(Grade):
输出Grade中出现次数最多的Grade。
如果有多个价值出现一样多,并且没有别的价值了,那么输出Bad Mushroom。
否则输出出现频率最高的价值,按照升序输出。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    int n, w, a[10017];
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("Case #%d:\n",++cas);
        memset(a,0,sizeof(a));
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&w);
            int tt = 10000-(100-w)*(100-w);
            a[tt]++;
        }
        int maxx = 0;
        for(int i = 0; i <= 10000; i++)
        {
            if(a[i] > maxx)
                maxx = a[i];
        }
        int flag = 1;
        int k = 0;
        for(int i = 0; i <= 10000; i++)
        {
            if(a[i] != maxx && a[i]>0)
            {
                flag = 0;
            }
            if(a[i] == maxx)
                k++;
        }
        if(flag && k > 1)//还有自身
        {
            printf("Bad Mushroom\n");
            continue;
        }
        flag = 0;
        for(int i = 0; i <= 10000; i++)
        {
            if(a[i] == maxx)
            {
                if(flag)
                    printf(" %d",i);
                else
                {
                    printf("%d",i);
                    flag = 1;
                }
            }
        }
        printf("\n");
    }
    return 0;
}


HDU 5038 Grade(数学)