HDU-1004 Let the Balloon Rise (STL map)

一时水题一时爽,一直水题一直爽(比赛全是打铁场)

题意:

有n个例子,统计颜色出现个数。自然联想到map来映射统计(顺便就当写下map用法)

#include <map>
映射关系map<key,value> 利用key来映射到value上
map iterator迭代器,指向一个“键值对”,first代表"Key", second代表"value“. 例如iterator it = map<a,b>tmp ,it->first,it->second
思考map<string ,int>word 与 map<int ,map>word区别(构造)

完整代码:注意一下C++提交可能不能通过要G++才行

#include<cstdio>
#include<iostream>
#include<map>//引入map容器
using namespace std;
int main()
{
    int n,num;
    string s,ans;
    while(cin>>n&&n)
    {
        num=-10;//num来记录最大值(取一个较小的数最为min)
        map<string ,int >word;//声明一个关键字为字符串值为数字的map
        map<string ,int>::iterator it;//迭代器 
        while(n--)
        {
            cin>>s;
            word[s]++;//关键字的值加一
        }
        for(it=word.begin();it!=word.end();it++)
        {
            if(it->second>num)
            {
                num=it->second;
                ans=it->first;
            }
        }
        cout<<ans<<endl;
    }
}
原文地址:https://www.cnblogs.com/Tianwell/p/11195479.html