统计数字

Description

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

Input

输入包含n+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

40%的数据满足:1<=n<=1000

80%的数据满足:1<=n<=50000

100%的数据满足:1<=n<=200000,每个数均不超过1500 000 000(1.5*109)

题目分析

就是统计数字出现的次数

解题思路

因为我们是hash专栏,所以这题我们也可以用hash来做
我们可以用一个hash存数,一个hash存次数
然后快排处理再输出

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
const int M=100007;
int t,hash[M][2];
struct node
{
   
	int ans,sum;
}a[M];
int h(int x)
{
   
	return x%M;
}
int dw(int x)//定位
{
   
	int i=0,o=h(x);
	while(i<M&&hash[(o+i)%M][0]&&hash[(o+i)%M][0]!=x) 
	 i++;
	return (o+i)%M;
}
bool cmp(node x,node y)//结构体快排
{
   
	return x.ans<y.ans;
}
int main()
{
   
	int n;
	scanf("%d",&n); 
	for(int i=1;i<=n;i++)
	{
   
		int x;
		scanf("%d",&x);
		hash[dw(x)][0]=x;//赋值
		hash[dw(x)][1]++;//次数
	}
	for(int i=0;i<M;i++)
	 if(hash[i][1])//判断是否出现过
	 {
   
	 	t++;//记录
	 	a[t].ans=hash[i][0];
		a[t].sum=hash[i][1];
	 }
	sort(a+1,a+t+1,cmp);//快排
	for(int i=1;i<=t;i++)//输出
	 cout<<a[i].ans<<' '<<a[i].sum<<endl;
	return 0;
} 

谢谢