HDU 1004 Let the Balloon Rise(Map)

數據統計的題目麼……


規定要用map ,就用了……


map的特殊之處在於其可以用string之類的數據類型來做序號,從而可以達到自制定義數組的要求。

本題就是構造的一個<string,int>的map。

然後遍歷一遍就行了。


最後AC Memory : 392K   Time : 0MS


代碼如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>

using namespace std;
int main()
{
    map<string,int> co; //構造需要的map
    map<string,int>::iterator iter; // 迭代器
    int N;
    while(scanf("%d",&N)&&N)
    {
        for(int i = 0;i<N;++i)
        {
            string str;
            cin>>str;
            iter = co.find(str);
            if(iter != co.end()) // 判斷該種顏色有沒有出現過在此map中
                iter->second++; //出現的話,對應的數 加一
            else
                co.insert(pair<string, int>(str, 1));// 沒出現過的話,就在map中添加這樣一條
        }
        int max = 0;
        string col;
        for (iter = co.begin(); iter != co.end(); iter++) // 找出最大的
        {
            if(iter->second >max)
            {
                max = iter->second;
                col = iter->first;
            }
        }
        cout<<col<<endl;
        co.clear();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章