蒜頭君面試 【SLT 中 Map 的簡單應用】

題解:就是 Map 一下,臨時存數,邊比較,邊輸入。

(編譯器壞掉了,只能網頁來寫,代碼好醜。)

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    int Max = -1;
    int numMax = 0;
    cin >> n;
    map<int,int>mp;
    int x;
    mp.clear();
    for(int i = 0; i < n; i ++){
        cin >> x;
        mp[x]++;
        if(mp[x] > numMax) {
            Max = x;
            numMax = mp[x];
        }
        else if(mp[x] == numMax && Max < x){
            Max = x;
        }
    }
    cout << Max << " " << numMax << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章