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
有n个蘑菇,重量为w[i],根据公式为这n个蘑菇分等级(给出一个分数),其中出现频率最多的那个分数成为mode,要求输出这个mode, 注意mode 可能不唯一,按升序输出符合是mode的分数。如果打出的分数每个都可以成为mode,那么则输出 Bad Mushroom.
可能上边说的不是很明白,为每个蘑菇打出分数后,有三种情况,举例子说明,假设有6个蘑菇:
1. 打出的分数为 2 2 2 1 1 6
其中2 出现的次数最多,mode唯一,输出 2
2. 打出的分数为 4 4 2 2 1 3
其中2,4出现的次数最多,mode不唯一,升序输出 2 4
3.打出的分数为 2 2 3 3 3 2
其中2,3出现的次数最多,但没有其它的分数了,也就是打出的分数每个都是mode,输出 Bad Mushroom.
其中第三种情况要特别注意一下,还要判断mode出现了几种,上面例子是出现了两种2,3,如果只出现一种,比如打出的分数为 2 2 2 2 2 2,要输出2,不能输出Bad Mushroom,所以要特判一下。
代码:
#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1000020;
int w[maxn];//重量
map<int,int>mp;//first 为求出的mode, second 为该mode出现的次数
int output[maxn];//输出
int n;
int main()
{
int t;cin>>t;
int c=1;
while(t--)
{
mp.clear();
scanf("%d",&n);
bool one=0;//只有一种mode
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
int temp=10000-(100-w[i])*(100-w[i]);
mp[temp]++;
if(mp[temp]==n)
one=1;
}
int maxn=-1;//找mode出现最多的次数
map<int,int>::iterator i;
for(i=mp.begin();i!=mp.end();i++)
{
if((i->second)>maxn)
maxn=i->second;
}
int len=0;
i=mp.begin();
bool same=1;//判断mode出现的次数是否相同
int flag=i->second;
for(i=mp.begin();i!=mp.end();i++)
{
if((i->second)==maxn)
output[len++]=i->first;
if((i->second)!=flag)
same=0;
}
cout<<"Case #"<<c++<<":"<<endl;
if(same&&!one)//当出现的mode的次数相同且mode的种类不唯一时
{
cout<<"Bad Mushroom"<<endl;
continue;
}
for(int i=0;i<len-1;i++)
cout<<output[i]<<" ";
cout<<output[len-1]<<endl;
}
return 0;
}
原文地址:http://blog.csdn.net/sr_19930829/article/details/39472127