[SOJ]统计数字

Description

某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

Input

    包含多个测试数据,每个包含n+1行:
    第1行是整数n,表示自然数的个数。
    第2~n+1行每行一个自然数。
    1<=n<=200000,每个数均不超过1 500 000 000(1.5*109)

Output

对每个测试数据输出m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。

相邻两个测试数据间用一个空行隔开。

Sample Input
 Copy sample input to clipboard
8
2
4
2
4
5
100
2
100
Sample Output
2 3
4 2
5 1
100 2

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int *a=new int[n+1];

        for(int i=0;i<n;i++)
            cin>>a[i];

        sort(a, a+n);  //对给定区间元素进行排序 
        a[n]=0;        //方便比较结束

        int cnt=1;

        for(int i=0;i<n;i++)
            if(a[i]!=a[i+1])
            {
                cout<<a[i]<<" "<<cnt<<endl;
                cnt=1;
            }
            else cnt++;
    
        delete[] a;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/KennyRom/p/6247868.html