【HDU1004】Let the Balloon Rise(stl map水过,哈希表)

Categories: 数据结构和算法
Comments: No Comments
Published on: 2011 年 09 月 15 日

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.

题意:给你一堆颜色的气球,让你统计处什么颜色的气球数量最多,输出该颜色。
题解:简单的数据结构哈希表的应用。之前写过不少,不太明白了看数据结构分类里边的哈希表部分

#include<iostream>
#include <map>
#include <string>
#include<algorithm>
using namespace std;
const int MAX=10010;
int main()
{
	//freopen("1.txt","r",stdin);
	int n;
	char col[20];
	while(cin>>n)
	{
		if(n==0) break;
		map<string,int> balloons;
		for (int i=0;i<n;i++)
		{
			cin>>col;
			balloons[col]++;
		}
		map<string,int>::iterator p,m;
		p=m=balloons.begin();
		for (p;p!=balloons.end();p++)
		{
			if(p->second > m->second) m=p;
		}
		cout<<m->first<<endl;
	}
}

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期五, 2017 年 12 月 15 日