試題編號: | 201503-2 |
試題名稱: | 數字排序 |
時間限制: | 1.0s |
內存限制: | 256.0MB |
問題描述: |
問題描述
給定n個整數,請統計出每個整數出現的次數,按出現次數從多到少的順序輸出。
輸入格式
輸入的第一行包含一個整數n,表示給定數字的個數。
第二行包含n個整數,相鄰的整數之間用一個空格分隔,表示所給定的整數。 輸出格式
輸出多行,每行包含兩個整數,分別表示一個給定的整數和它出現的次數。按出現次數遞減的順序輸出。如果兩個整數出現的次數一樣多,則先輸出值較小的,然後輸出值較大的。
樣例輸入
12
5 2 3 3 1 3 4 2 5 2 3 5 樣例輸出
3 4
2 3 5 3 1 1 4 1 評測用例規模與約定
1 ≤ n ≤ 1000,給出的數都是不超過1000的非負整數。
|
#include <iostream>
#include <cstdio>
using namespace std;
struct KEY_VALUE
{
int Key;
int Value;
};
int main()
{
int i;
int n,maxn=0;
int temp;
KEY_VALUE su[1005];
///init
for(i=0; i<1005; i++)
{
su[i].Value=0;
su[i].Key=0;
}
///input
cin>>n;
for(i=1; i<=n; i++)
{
cin>>temp;
su[temp].Value++;
su[temp].Key=temp;
if(temp>maxn)maxn=temp;
}
///sort
for (int i = 0; i < maxn+1; i++)
for (int j = maxn+1 ; j >= i; j--)
{
if (su[j].Value < su[j+1].Value)
{
temp = su[j+1].Value;
su[j+1].Value = su[j].Value;
su[j].Value = temp;
temp = su[j+1].Key;
su[j+1].Key = su[j].Key;
su[j].Key = temp;
}
else if(su[j].Value == su[j+1].Value)
if(su[j].Key > su[j+1].Key)
{
temp = su[j+1].Key;
su[j+1].Key = su[j].Key;
su[j].Key = temp;
}
}
///output
for(i=0; i<maxn+1; i++)
{
if(su[i].Value==0)break;
cout<<su[i].Key<<" "<<su[i].Value;
if(i!=(maxn))
cout<<endl;
}
return 0;
}