HDOJ 5038 Grade (模拟)

来源:互联网 发布:php授权系统源码下载 编辑:程序博客网 时间:2024/06/05 16:50

Grade

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1450    Accepted Submission(s): 615


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
36100 100 100 99 98 1016100 100 100 99 99 1016100 100 98 99 99 97
 

Sample Output
Case #1:10000Case #2:Bad MushroomCase #3:9999 10000


题意:用给出的公式求出每个蘑菇的grade,求出现次数最多的grade。如果有多个grade出现的次数一样多,且还有其他的grade,则把这些出现次数最多的grade按升序输出;否则,输出“Bad Mushroom”。

思路:模拟就行了

总结:刚开始看错了题,写了好几次都没写对,最后看了discuss才发现理解错了题意


ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 100100#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)using namespace std;int num[MAXN];int ans[MAXN];int main(){int t,a,i,n;int cas=0;scanf("%d",&t);while(t--){scanf("%d",&n);mem(num);int M=-1,mi=INF;for(i=0;i<n;i++){scanf("%d",&a);int b=10000-(100-a)*(100-a);mi=min(mi,b);M=max(M,b);num[b]++;}int mm=-1;for(i=mi;i<=M;i++)mm=max(mm,num[i]);int cnt=0;int k=0;for(i=mi;i<=M;i++){if(num[i])k++;if(mm==num[i]){ans[cnt++]=i;}}printf("Case #%d:\n",++cas);if(k*mm==n&&k>1)printf("Bad Mushroom\n");else{sort(ans,ans+cnt);for(i=0;i<cnt-1;i++)printf("%d ",ans[i]);printf("%d\n",ans[i]);}}return 0;}


0 0
原创粉丝点击