用STL解决Let the Balloon Rise这道题的妙处

[size=medium]Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink

code as following
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int nCount;
while(cin>>nCount && nCount)
{
string color;//存放输入的颜色的临时变量
map<string,int> mapBalloon;//用map容器存放气球颜色及出现次数
while(nCount--)
{
cin>>color;//输入颜色
mapBalloon[color]++;
//因为是使用数组方式存入map容器,当输入的颜色已存在于容器关键字中时,不会重复存入容器,
//容器中的值是自动初始化为0的,所以这里只把此颜色关键字对应的值+1
}
map<string, int>::iterator iter;//遍历
int max=0;
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第一次遍历标记最大次数所在位置
if(iter->second > max)
max = iter->second;
}
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第二次遍历找到标记的位置
if(iter->second == max)
cout<<iter->first<<endl;
}
}
return 0;
} [/size]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值