CCF考试认证模拟练习——数字排序

#include<iostream>
#include<algorithm>
using namespace std;
struct node
//定义结构体
{
int num;//数据
int count;//出现的次数 计数
};
bool cmp(node a, node b)//排序
{
if (a.count != b.count)
return a.count>b.count;
return a.num<b.num;
}
node a[1005];
//node temp;
int main()
{
int n,x,i;
for ( i = 0; i <= 1000; ++i)
{
a[i].num = i;
}
cin >> n;
for (i = 1; i <= n; ++i)
{
cin >> x;
++a[x].count;
}
sort(a, a + 1005, cmp);
for ( i = 0; i <= n; ++i)
{
if (a[i].count == 0)
break;
cout << a[i].num << " " << a[i].count << endl;
}
return 0;

}

posted @ 2016-08-06 19:26  A-inspire  Views(1174)  Comments(0Edit  收藏  举报