zoj 2104 Let the Balloon Rise(map映照容器的应用)

2022-12-22,,,

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2104

题目描述:

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input

Input contains multiple test cases. Each test case starts with a number N (0
< N < 1000) -- the total number of balloons distributed. The next N lines
contain one color each. The color of a balloon is a string of up to 15 lower-case
letters.

A test case with N = 0 terminates the input and this test case is not to be
processed.

Output

For each case, print the color of balloon for the most popular problem on a
single line. It is guaranteed that there is a unique solution for each test
case.

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

 /*问题 统计每种颜色气球的个数并输出
解题思路 问题本身很简单,使用结构体数组也可以做,这里提供一种map映照容器的做法,效率更高
具体做法:每读入一个字符串,先搜索该键值的是否存在,存在修改映照数据,不存在插入,最后遍历找到映照数据
最大即可*/
#include <cstdio>
#include <map>
#include <string>
using namespace std; int main()
{
int n;
char s[];
string colo;
map<string,int> m;
map<string,int>::iterator it,max;
while(scanf("%d",&n), n != )
{
while(n--)
{
scanf("%s",s);
colo=s;
it=m.find(colo);
if(it != m.end())
m[colo]++;
else
m[colo]=;
} max=m.begin();
for(it=m.begin(); it != m.end();it++){
if(it->second > max->second)
//if((*it).second > (*max).second)
max=it;
}
printf(max->first.c_str());//转换
putchar('\n');
m.clear();
}
return ;
}

zoj 2104 Let the Balloon Rise(map映照容器的应用)的相关教程结束。

《zoj 2104 Let the Balloon Rise(map映照容器的应用).doc》

下载本文的Word格式文档,以方便收藏与打印。