美文网首页
zerojudge b374: [福州19中]众数

zerojudge b374: [福州19中]众数

作者: iamkai | 来源:发表于2016-11-18 17:55 被阅读0次

Problem

http://zerojudge.tw/ShowProblem?problemid=b374

Thinking

開一個表格計數所有數字出現的個數,然後從表格找出最常出現的數,因為可能會有出現次數一樣的問題,在最後要再檢查其他數字的數量是否跟眾數一樣,一樣則輸出

Code

#include <iostream>
using namespace std;

int main(){
    int length;
    
    while(cin >> length)
    {
        int count[30001] = {0};
        int temp;
        
        for(int i = 0 ; i < length ; i++)
        {
            cin >> temp;
            count[temp]++;
        }
        
        int max = -1;
        for(int i = 0 ; i < 30001 ; i++)
        {
            if(count[i] > max)
                max = count[i];
        }
        //一樣次數的也要輸出    
        for(int i = 0 ; i < 30001 ; i++)
        {
            if(count[i] == max)
                cout << i << " " << count[i] << endl;
        }
        
    }
}

Reference

1.<<高中生解題系統-參考答案>>

相关文章

网友评论

      本文标题:zerojudge b374: [福州19中]众数

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