【題目敘述】https://zerojudge.tw/ShowProblem?problemid=b162
【解題想法】map/dictionary, sorting
#include <iostream>
#include <map>
using namespace std;
map<int,int> mp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, x;
cin >> n;
for (int i = 0; i < n; i++){
cin >> x;
mp[x]++;
}
for (auto i: mp){
cout << i.first << " " << i.second << "\n";
}
return 0;
}
Python code (credit: Amy Chou)
n = int(input())
dic = {}
for i in range(n):
x = int(input())
dic[x] = dic.get(x, 0) + 1
for k in sorted(dic.keys()):
print(k, dic[k])