您的位置:首页 > 其它

【map练习】【map+模拟】codevs1164 统计数字题解

2015-10-15 20:03 465 查看
题目描述 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)

stl大法好,map模拟666

代码:

//codevs1164 ͳ¼ÆÊý×Ö Ä£Äâ
//copyright by ametake
#include
#include
#include
#include
using namespace std;

const int maxn=200000+10;
map m;
int n;

inline void read(int &a)
{
int f=1;
a=0;
char ch=getchar();
while (ch<'0'||ch>'9')
{
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9')
{
a=(a<<1)+(a<<3)+ch-'0';
ch=getchar();
}
a*=f;
return;
}

int main()
{
read(n);
int x;
m.clear();
for (int i=1;i<=n;i++)
{
read(x);
m[x]++;
}
for (map::iterator it=m.begin();it!=m.end();it++)
{
printf("%d %d\n",it->first,it->second);
}
while (1);
return 0;
}


注意迭代器的代码

for (map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
printf("%d %d\n",it->first,it->second);
}

——官仓老鼠大如斗,见人开仓亦不走
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  map stl 模拟 基础算法