PAT-A 1054. The Dominant Color (20)

本文介绍了一个简单的C++程序,用于解决寻找二维数组中出现次数超过总数一半的数字的问题。通过使用标准模板库中的map来记录每个数字出现的次数,进而找到满足条件的众数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目连接在此

题意:
给出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;
    }
利用matlab识别[-37.6141564375877 -44.8934318146598 -68.5395102231449 -105.998636800599 -39.0202173875387 -15.0676705979633 -70.0289155856468 -89.6183719712618 -31.4998736017068 -65.6706070347323 -52.2954712582728 -25.0007417656538 -62.0175753680824 -52.4505190780410 -36.7998214080831 -66.8240803995901 -89.7310248077318 -89.4476631841424 -95.0631484078666 -45.4266150893948 -71.4268623418806 -87.7389305484529 -90.7689607725678 -89.4482765744236 -63.4873672376004 -50.5822037813285 -89.0051497735307 -104.630351899095 -71.8133034594315 -87.1409454746534 -101.322624957044 -96.8859043636271 -107.236700645133 -102.635430504787 -85.8695232644000 -88.0600512107977 -91.2951358823337 -74.5224644056697 -81.4289479344271 -92.8546062893397 -64.3662613256191 -60.7190970833584 -3.55577647563955 -44.7515582179228 -55.2621821199571 -58.1191843564058 -64.7337352687441 -58.0577833197574 -46.8047623917448 -45.1175093264491 -46.4333171948036 -57.5279718954082 5.02150694070810 -23.4266564799937 -0.389119636882192 10.3895229106278 3.83098733011624 -1.66067566291584 7.99355669801910 19.6746491807298 26.0543573655568 15.3122579508032 13.5242208685403 2.98885682332441 13.4193364621863 2.55985415353877 60.7869486491908 -7.19843777126744 50.0025300398218 -19.2306656104956 84.7794322602891 45.4102510636858 116.883089912579 102.259133619137 14.0774708419592 28.4068229352914 27.8204958611579 31.4538966828311 93.2036965898183 -2.61208839117787 -38.6618143537309 -23.3092017890169 -15.6724244605204 -37.2371241010961 20.3178013223489 49.1877327947727 -16.3352241625370 44.4518673391411 34.8328106589607 -3.39617598124692 4.22761052220510 -127.033670095793 -106.268209096416 -86.7501347324323 -44.1581155090221 -89.0199577753159 -75.9603939084827 -78.8226974792923 -103.391357116246 -115.267742416180 -45.1877231021113 -89.3961504838651 -51.5825019761869 -55.0672415959877 -62.6021257843332 -63.5984516565330 -65.8130844153139 -58.2838155448689 -93.8797357975003 -73.5215320794161 -116.822323814052 -62.761410181]的特征
最新发布
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值