题目:统计数字(map的用法和迭代器)

题目描述

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

输入

第1行是整数n,表示自然数的个数;
第2~n+l每行一个自然数。

输出

共m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
 
样例输入
8
2
4
2
4
5
100
2
100

样例输出

2 3
4 2
5 1
100 2
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
#define ll long long
using namespace std;
int main()
{
    ll n;cin >> n;ll x;
    map<ll, ll> con;
    while (n--)
    {
        cin>>x;
        ++con[x];
        //m不在map中是会自动添加,键为m值为0。
    }
    map<ll,ll>::iterator it;//
    for ( it = con.begin(); it != con.end(); ++it)
    {
        cout << it->first << ' ' << it->second << endl;
    }
    return 0;
}

 

 
posted @ 2019-01-24 10:23  轩瑞  阅读(173)  评论(0编辑  收藏  举报
Live2d Test Env