题目连接在此。
题意:
给出n行n列个数,求其中出现次数最多且超过所有数字个数一半的数字。
#include<iostream>
#include<map>
using namespace std;
map<int,int> tbl; //map<a,b>表示数字a出现了b次
int main(){
int m,n;
cin >> m >> n;
int count = 0; //count记录输入总个数
int temp;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> temp;
tbl[temp] ++; //temp对应个数+1
}
}
int max = 0, res = -1; //max为最大出现次数, res为最大出现次数对应的数字
int half = count/2; //总个数的一半
for(map<int,int>::iterator it = tbl.begin(); it != tbl.end(); it++){
if(it->second > max && it->second > half){ //如果次数超过当前最大值,并且多于当前个数的一半
max = it->second; //更新当前最大次数
res = it->first; // 更新当前结果
}
}
cout << res;
return 0;
}