美文网首页
2020-02-16

2020-02-16

作者: km15 | 来源:发表于2020-02-16 18:34 被阅读0次

/*
题意:给一个列、行的二维矩阵,统计最多数字

解题:
1、用一个哈希二维数组呗,
2、但是如何统计最大呢,遍历?会超时吗,

learn && wrong:
1、iterator写错,以及两个冒号是放在变量前
2、题意很好,一个数字,一个映射次数,
3、查找,find跟end.()结合,这招出现很多次了
4、查找最大,max为0,然后it->second做比较
*/

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;

int main()
{
    int n,m,col;
    scanf("%d%d", &n,&m);   //行与列
    map<int,int> count; //数字与出现的次数map映射

    for(int i = 0;i < n;++i){
        for(int j = 0;j < m;++j){
            scanf("%d",&col);   //输入数字
            if(count.find(col) != count.end()) count[col]++;    //若已经存在,则次数+1   !!!
            else count[col] = 1;    //若不存在,则次数置为1
        }
    }
    int k = 0,max = 0;  //最大数字以及出现的次数   !!!
    for(map<int,int>::iterator it = count.begin();it != count.end();++it){
        if(it->second > max){
            k = it->first;  //最大的数字
            max = it->second;   //出现的次数;
        }
    }
    cout<<k<<endl;
    return 0;
}

相关文章

网友评论

      本文标题:2020-02-16

      本文链接:https://www.haomeiwen.com/subject/dmxmfhtx.html