矩阵中出现次数最多的数
输入:
5 3
1 2 4 5 2
2 4 5 3 1
5 6 8 4 2
输入:
2
#include <cstdio>
#include<iostream>
#include <map>
using namespace std;
int main()
{
int N, M, col;
cin >> N >> M;
map<int, int>m;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> col;
if (m.find(col) != m.end()) //若已存在,则次数加1
m[col]++;
else
m[col] = 1; //若不存在,则置1
}
}
int k = 0, max = 0;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
if (it->second > max)
{
k = it->first;
max = it->second;
}
}
cout << k << endl;
return 0;
}
浙公网安备 33010602011771号