HDU 1004 Let the Balloon Rise

题目链接:HDU 1004 Let the Balloon Rise

题目大意:
给你\(N\)个颜色不同的气球让你从中找到出现次数最多的那种颜色,输出出现次数最多的那种颜色。

题解:
对每个颜色进行计数,用map解决。

#include <iostream>
#include <string>
#include <map>
using namespace std;

int n, cnt;
map <string, int> mat;
string color, ans;

int main() {
	while (cin >> n && n) {
		mat.clear();
		while (n--) {
			cin >> color;
			mat[color]++;
		}
		cnt = 0;
		for (map <string, int>::iterator iter = mat.begin(); iter != mat.end(); ++iter) {
			if (iter->second > cnt) {
				cnt = iter->second;
				ans = iter->first;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
posted @ 2020-10-15 00:10  ZZHHOOUU  阅读(119)  评论(0编辑  收藏  举报