题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5038
题目:
Grade
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3292 Accepted Submission(s): 1193
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.
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.
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”.
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
题意:
1.只有一种最高频的grade,直接输出
2.所有grade的出现的频率都相同,输出Bad Mushroom
3.有多个最高频grade,除去这些grade还有一些低频grade
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 int n; 7 int visited[205]; 8 struct node{ 9 int v; 10 int t; 11 }num[205]; 12 int getValue(int i){ 13 return 10000-(100-i)*(100-i); 14 } 15 bool cmp(node x,node y){ 16 return x.t>y.t; 17 } 18 vector<int>v; 19 int main(){ 20 int t,a,b; 21 scanf("%d",&t); 22 for (int i=1; i<=t; i++) { 23 int flag=0; 24 for (int j=0; j<=200; j++){ 25 num[j].t=0; 26 num[j].v=j; 27 } 28 v.clear(); 29 memset(visited, 0, sizeof(visited)); 30 printf("Case #%d:\n",i); 31 scanf("%d",&n); 32 for (int j=0; j<n; j++) { 33 scanf("%d",&a); 34 visited[a]++;//统计重量为a的蘑菇出现了几次 35 } 36 for (int j=0; j<100; j++) num[j].t=visited[j]+visited[200-j];//重量为j的grade和重量为200-j的grade是一样的,就合并计算 37 num[100].t=visited[100]; 38 sort(num,num+105,cmp); 39 int len=1,l; 40 for (l=1; l<=101 && num[l].t!=0; l++);//找总共有几个grade 41 for (len=1; len<=101 && num[len].t==num[len-1].t; len++);//找最高频grade的数量 42 if(len==1) printf("%d\n",getValue(num[0].v));//情况1,只有一个最高配grade 43 else if(len==l) printf("Bad Mushroom\n");//情况2,最高配grade数量等于总数 44 else{//情况3,算出grade 并排序升序输出 45 for (int j=0; j<len; j++) { 46 int x=getValue(num[j].v); 47 v.push_back(x); 48 } 49 sort(v.begin(), v.end()); 50 printf("%d",v[0]); 51 for (int j=1; j<v.size(); j++) { 52 printf(" %d",v[j]); 53 } 54 printf("\n"); 55 } 56 } 57 return 0; 58 }