【map練習】【map+模擬】codevs1164 統計數字題解

題目描述 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);
    }

——官倉老鼠大如鬥,見人開倉亦不走



發佈了153 篇原創文章 · 獲贊 15 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章