PAT (Advanced) 1054. The Dominant Color (20)

方法一:使用map

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> mp;
map<int, int> ::iterator it;

int main()
{
	int m, n;
	cin >> m >> n;
	for (int i = 0; i < m * n; i++)
	{
		int tmp;
		scanf("%d", &tmp);
		mp[tmp]++;
	}
	for (it = mp.begin(); it != mp.end(); ++it)
	{
		if (it->second > m * n / 2)
		{
			cout << it->first << endl;
			break;
		}
	}
	return 0;
}

方法二:模拟多队pk

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

int main()
{
	int m, n;
	cin >> m >> n;
	int result, color, remain = 0;
	for (int i = 0; i < m * n; i++)
	{
		scanf("%d", &color);
		//cin >> color;
		if (remain == 0)
		{
			result = color;
			remain++;
		}
		else
		{
			if (color == result)
				remain++;
			else
				remain--;
		}
	}
	cout << result << endl;

	return 0;
}

你可能感兴趣的:(PAT (Advanced) 1054. The Dominant Color (20))