读书人

HDOJ 1004 Let the Balloon Rise (地图

发布时间: 2012-09-17 12:06:51 作者: rapoo

HDOJ 1004 Let the Balloon Rise (map)

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

题意:求出现次数最多的颜色。

思路:用map存储颜色和出现次数,然后遍历一遍查找出现次数最多的即可。

#include<iostream>#include<string>#include<map>using namespace std;int main(){    int n;    map <string,int> color;    while(cin>>n&&n)    {        color.clear();        while(n--)        {            string ip;            cin>>ip;            if(color.find(ip)!=color.end())                color[ip]++;            else                color[ip]=1;        }        map<string,int>::iterator p;        string op;        int max=0;        for(p=color.begin();p!=color.end();p++)        {            if(p->second>max)            {                op=p->first;                max=p->second;            }        }        cout<<op<<endl;    }    return 0;}

读书人网 >编程

热点推荐