Description
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5109)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。
Input
输入包含n+1行;第一行是整数n,表示自然数的个数,1<=n<=200000;第2~n+1每行一个自然数,每个数均不超过1 500 000 000(1.5109)。
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
ps:这道题,,我一开始用结构体,t了,然后用for镶嵌,也t了,后来改进了一下,循环输出,A掉了,But,我的代码是别人的两倍长, 后来我就搜了一下,发现,,噢噢,,原来可以用map,马上去捡丢掉的map,…特写此文,以慰map。然后,我发现用map代码 长度也差不多。。。。。
好了,下面上我的for代码
int a[200005];
int main( )
{
int n, bu = 0;
cin >> n;
for(int i=0; i<n; ++i)
{
scanf("%d", &a[i]);
}
sort(a, a+n);
for(int i=0; i<n-1; i++)
{
bu++;
if(a[i+1] != a[i])
{
cout << a[i] << ' ' << bu << '\n';
bu = 0;
}
}
if(a[n-2] == a[n-1])
cout << a[n-1] << ' ' << bu+1 << '\n';
else
cout << a[n-1] << ' ' << 1 << '\n';
return 0;
}
下面我们用map:
int main( )
{
int n;
cin >> n;
map<int, int>same;
int x;
for(int i=0; i<n; i++)
{
cin >> x;
++same[x];
}
map<int, int>::iterator iter;
for(iter=same.begin(); iter != same.end(); iter++)
{
cout << iter->first << ' ' << iter->second << '\n';
}
return 0;
}
注意加头文件