Let the Balloon Rise(map)

题意:给你一个数字 N ,代表有N个气球,让你求出出现次数最多的气球的颜色

解题思路:使用map,然后迭代器进行访问,比较输出次数最多的颜色即可

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

 AC代码

#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
//#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,max;
	string s,ans;
	map<string,int>p;
	while(cin>>n)
	{
		if(n==0)
		break;
		max=0;
		p.clear();					//清空 
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			p[s]++;					//把出现的次数存入 
		}
		map<string,int>::iterator it;		//定义迭代器 
		for(it=p.begin();it!=p.end();it++)		//遍历 
		{
			if((*it).second>max)
			{
				max=(*it).second;			//次数取最大值 
				ans=(*it).first;			//次数最大时的字符串s 
			}
		}
		cout<<ans<<endl;
	}
	return 0;
} 

努力努力再努力

猜你喜欢

转载自blog.csdn.net/zz_xun/article/details/119838552