
解题思路
用map容器存储输入得值和次数,然后用优先队列得结构来输出。
重点是优先队列中的优先级比较需要关注一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<iostream> #include<map> #include<queue> using namespace std; struct node{ int key; int count; friend bool operator <(node n1,node n2){ if(n1.count == n2.count){ return n1.key>n2.key; } else return n1.count <n2.count; } }; int main(){ int n; cin>>n; map <int, int> number; priority_queue <node> q; int i; int num; for(i=0;i<n;i++){ cin>>num; number[num]++; } node tmp; for(map<int, int>::iterator it = number.begin();it!=number.end();it++){ tmp.key = it->first; tmp.count = it->second; q.push(tmp); } while(!q.empty()){ tmp = q.top(); cout<<tmp.key<<" "<<tmp.count<<endl; q.pop(); } return 0; }
|
近期评论