#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
//Time Limit: 1.0s, Memory Limit: 256.0M
struct Elem {
int key;
int value;
bool operator < (const Elem &elem) {
if (elem.value == value) {
return elem.key > key;
}
else
return elem.value < value;
}
};
void main() {
int n;
int input;
vector<Elem> outcome;
map<int, int> record;
map<int, int>::iterator it;
cout << "请输入整数个数:";
cin >> n;
//存入map
cout << "请输入整数:" << endl;
for (int i = 0; i < n; i++) {
cin >> input;
record[input]++;
}
//存入vector<Elem>
for (it = record.begin(); it != record.end(); it++) {
Elem elem;
elem.key = it->first;
elem.value = it->second;
outcome.push_back(elem);
}
sort(outcome.begin(),outcome.end());
for (int i = 0; i < outcome.size(); i++) {
cout << outcome[i].key << " " << outcome[i].value << endl;
}
}
求解数字排序问题
最新推荐文章于 2024-08-02 18:20:17 发布