码迷,mamicode.com
首页 > 其他好文 > 详细

ccf 201312-1 出现次数最多的数

时间:2020-06-19 20:47:36      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:数组下标   space   int   index   c++   color   png   div   style   

技术图片

 

思路

用数组下标作为数字,数组元素记录次数

注意事项

声明数组的时候,如果用

int index[10000];

会出现不为0的随机数

#include<bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int n, num;
    int index[10000] = {};    
    cin >> n;
    // 记录每个数的出现次数 
    for(int i = 0; i < n; i++){
        cin >> num;
        index[num] += 1;
    }
    
    // 记录最大数
    queue<int> q;        
    
    // 找出出现次数最大的数字
    int max = 0;
    for(int i = 0; i < 10000; i++) {
        if(index[i] > 0){                    
            if(index[i] > max){
                max = index[i];
                queue<int> empty;
                swap(empty, q);            
                q.push(i);                
            }else if(index[i] == max){
                q.push(i);
            }
        }        
    }
    // 检查队列的长度
    if(q.size() == 1){
        cout << q.front() << endl;
    }else if(q.size() > 1){
        int min = q.front();
        q.pop();
        while(!q.empty()){
            int p = q.front();
            q.pop();
            if(p < min){
                min = p;
            }
        }
        cout << min << endl;
    }
    return 0;
}

 

ccf 201312-1 出现次数最多的数

标签:数组下标   space   int   index   c++   color   png   div   style   

原文地址:https://www.cnblogs.com/YC-L/p/13166273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!