题目描述 Description
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数
不超过10000 个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统
计结果。
输入描述 Input Description
第1行是整数n,表示自然数的个数。
第2~n+1 行每行一个自然数。
输出描述 Output Description
输出包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大
的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
样例输入 Sample Input
8
2
4
2
4
5
100
2
100
样例输出 Sample Output
2 3
4 2
5 1
100 2
数据范围及提示 Data Size & Hint
【限制】
40%的数据满足:1<=n<=1000
80%的数据满足:1<=n<=50000
100%的数据满足:1<=n<=200000,每个数均不超过1 500 000 000(1.5*10^9)
思路
水的不行,十分钟。
可以用map更简单。
我直接模拟的。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N=200000+5;
int n,line[N],tot=1;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)
line[i]=read();
sort(line+1,line+n+1);
printf("%d ",line[1]);
if (n==1) printf("1");
else
{
for (int i=2;i<=n;i++)
{
if (line[i]==line[i-1]) tot++;
else {printf("%d\n",tot);printf("%d ",line[i]);tot=1;}
if (i==n) printf("%d\n",tot);
}
}
return 0;
}