本文主要是介绍数次统计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- Description
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。
- Input
多组输入数据
每组数据包含n+1行:第1行是整数n,表示自然数的个数。第2~n+1行每行一个自然数。
- Output
每组数据输出包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
- Sample Input
8
2
4
2
4
5
100
2
100
- Sample Output
2 3
4 2
5 1
100 2
- Hint
数据满足:1<=n<=200000,每个数均不超过1 500 000 000(1.5*109)
#include<iostream>
#include<algorithm>
using namespace std;
int a[200001];
int main()
{
// freopen("in.txt","r",stdin);int n;while(scanf("%d",&n)!=EOF){int i,count;for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);i=0;while(i<n){count=1;while(a[i]==a[i+1])i++,count++;printf("%d %d\n",a[i],count);i++;}}return 0;
}
这篇关于数次统计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!