【map热手题】HDU 1004—Let the Balloon Rise

题目:点击打开链接

建议初学STL的人用MAP来切这道水题,可以对MAP的方便之处有更加深刻的理解。

因为调用和查找的确挺简单的。效率据查阅资料,是lgN.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
	int ballnum;
	while(cin>>ballnum && ballnum!=0)
	{
		string temp;
		int bigger=0;
		map<string,int> balloon;
		
		for(int i=0;i<ballnum;i++)
		{
			cin>>temp;
			balloon[temp]++;
		}
		
		map<string,int>::iterator it;
		for(it=balloon.begin();it!=balloon.end();it++)
		{
			if((*it).second>bigger)
			{
				bigger=(*it).second;
			}
		}
		
		for(it=balloon.begin();it!=balloon.end();it++)
		{
			if((*it).second==bigger)
			{
				cout<<(*it).first<<endl;
			}
		}
	
		
		
		
		
		
	}
	
	return 0;
}


你可能感兴趣的:(【map热手题】HDU 1004—Let the Balloon Rise)