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++】
1 #include<cstdio> 2 #include<algorithm> 3 struct point{ 4 int v, c; 5 }data[105]; 6 bool cmp(point A, point B){ 7 if (A.c ^ B.c) return A.c > B.c; 8 return A.v > B.v; 9 } 10 int main(){ 11 int T, i, j, N, nb, opt[101]; 12 for (j = 0; j < 101; ++j) opt[j] = 10000 - j*j; 13 while (~scanf("%d", &T)){ 14 for (i = 1; i <= T; ++i){ 15 printf("Case #%d: ", i); 16 scanf("%d", &N); 17 for (j = 0; j <= 100; ++j) data[j].c = 0, data[j].v = j; 18 for (j = 0; j < N; ++j){ 19 scanf("%d", &nb); 20 if (nb>100) nb -= 100; 21 else nb = 100 - nb; 22 ++data[nb].c; 23 } 24 std::sort(data, data + 102, cmp); 25 nb = data[0].c; 26 for (j = 0; data[j].c == nb; ++j) N -= data[j].c; 27 if (N || j == 1){ 28 printf("%d", opt[data[0].v]); 29 for (j = 1; data[j].c == nb; ++j) printf(" %d", opt[data[j].v]); 30 puts(""); 31 } 32 else puts("Bad Mushroom"); 33 } 34 } 35 return 0; 36 }
HDU 5038