自己写个排序的cmp。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct point{ int num,counts; }a[1005]; bool cmp(point x,point y) { if(x.counts == y.counts) return x.num < y.num; return x.counts > y.counts; } int main() { int n,all = 0; cin >> n; for(int i = 0;i <= 1000;i++) { a[i].num = i; a[i].counts = 0; } int temp; while(n--) { cin >> temp; if(!a[temp].counts) all++; a[temp].counts++; } sort(a,a+1001,cmp); for(int i = 0;i < all;i++) cout << a[i].num << ' ' << a[i].counts << endl; return 0; }