C++ 统计自然数出现次数并排序输出
#include
using namespace std;
int main() { int n; cin >> n;
map<int, int> countMap;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
countMap[num]++;
}
vector<pair<int, int>> countVec;
for (auto it = countMap.begin(); it != countMap.end(); it++) {
countVec.push_back(make_pair(it->first, it->second));
}
for (int i = 0; i < countVec.size(); i++) {
cout << countVec[i].first << ' ' << countVec[i].second << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qnld 著作权归作者所有。请勿转载和采集!