PAT甲级 1054 The Dominant Color (20分) (模拟)

题目链接:传送门

思路:直接存储数字出现次数,保存一个出现次数最大的数即可。(题意是选取占得面积大于50%的,但是其实选取最大的也行,应为题目应该是保证一定有大于50%的数)

代码:

#include <bits/stdc++.h>

using namespace std;

map <int , int> mp;


int main() {
	int n , m;
	ios::sync_with_stdio(0);
	cin >> m >> n;
	int mm = 0 , ans = 0;
	for(int i = 0 ; i < n ; i++) {
		for(int j = 0 ; j < m ; j++) {
			int t;
		    cin >> t;
		    mp[t]++;
		    int tmp = mp[t];
		    if(mm < tmp) { 
		    	mm = tmp;
				ans = t;
			}
		}
	}
	cout << ans << "\n";
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_39475280/article/details/103394095