Tanky WooRSS

HDOJ 1004 Let the Balloon Rise

24 Jul 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1004

看到题目第一眼就觉得c++的map来作为数据结构存储是想当方便的,

这里要了解c++的map的功能.map包括2个值,第一个是作为“下标”来看待的,这里可以存储颜色。

第二个当然就可以当做颜色出现的次数了。

代码:

 // Author: Tanky Woo
// HDOJ 1004
//


#include 
#include 
#include 
using namespace std;

map balloon;
string color;

int main()
{
    //freopen("input.txt", "r", stdin);
    int nTimes;
    while(cin>>nTimes && nTimes)
    {
        balloon.clear();     //注意要清空
        for(int i=0; i> color;
            balloon[color]++;    //map的用法要掌握
        }
        map::const_iterator iter;
        int _max = -1;
        string _record;  // 暂存最大值的颜色
        for(iter = balloon.begin(); iter != balloon.end(); ++iter)
        {
            if(iter->second > _max)
            {
                _max = iter->second;
                _record = iter->first;
            }
        }
        cout << _record << endl;
    }
    return 0;
}