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. |
Teb是Always Cook Mushroom (ACM)的员工。老板Matt给了他一包蘑菇叫他按质量分级。假设蘑菇的质量为w,那么它的等级为: s = 10000 - (100 - w)^2 并且,Teb还得报告这些蘑菇等级的众数。众数就是最常出现的值。众数可不唯一。如果所有值各不相同但出现的频率一样,则没有众数。 |
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. |
输入的首行是一个整数T表示测试样例的数量。接下来T个测试样例。 每个测试样例的第一行有一个整数N(1<=N<=10^6),表示蘑菇的数量。 第二行有N个整数,表示每个蘑菇的质量。质量大于0,小于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”. |
对于每个测试样例,输出2行。 第一行为"Case #x:",这里的x表示样例的序号(从1开始)。 第二行为给定蘑菇其等级的众数。如果存在多个众数,升序输出。如果不存在,则输出“Bad Mushroom”。 |
Sample Input - 输入样例 |
Sample Output - 输出样例 |
3 6 100 100 100 99 98 101 6 100 100 100 99 99 101 6 100 100 98 99 99 97 |
Case #1: 10000 Case #2: Bad Mushroom Case #3: 9999 10000 |
【题解】
注意Bad Mushroom的条件,然后是如果只有一个数,那么那个数即是那组数据的众数。
用sort排个序就出来了。
【代码 C++】
#include<cstdio>
#include<algorithm>
struct point{
int v, c;
}data[];
bool cmp(point A, point B){
if (A.c ^ B.c) return A.c > B.c;
return A.v > B.v;
}
int main(){
int T, i, j, N, nb, opt[];
for (j = ; j < ; ++j) opt[j] = - j*j;
while (~scanf("%d", &T)){
for (i = ; i <= T; ++i){
printf("Case #%d:\n", i);
scanf("%d", &N);
for (j = ; j <= ; ++j) data[j].c = , data[j].v = j;
for (j = ; j < N; ++j){
scanf("%d", &nb);
if (nb>) nb -= ;
else nb = - nb;
++data[nb].c;
}
std::sort(data, data + , cmp);
nb = data[].c;
for (j = ; data[j].c == nb; ++j) N -= data[j].c;
if (N || j == ){
printf("%d", opt[data[].v]);
for (j = ; data[j].c == nb; ++j) printf(" %d", opt[data[j].v]);
puts("");
}
else puts("Bad Mushroom");
}
}
return ;
}
HDU 5038