为大家介绍一种使用 map 的解法,代码很简洁。
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, t;
map<int, int> m;
cin >> n;
for(int i = 0; i < n ; i++) {
cin >> t;
m[t]++;
}
for(auto it = m.begin() ; it != m.end() ; it++) {
cout << it->first << ' ' << it->second << endl;
}
return 0;
} 
京公网安备 11010502036488号