Mr. Suantou Interview [Simple Application of Map in SLT]

Solution: Just click on a map, temporarily store the number, compare and input.

(The compiler is broken and can only be written on the web, the code is ugly.)

#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;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/106108059