代码改变世界

Let the Balloon Rise

2015-05-08 20:46  想打架的蜜蜂  阅读(986)  评论(0编辑  收藏  举报

先看看题目

这题让我崩溃了两小时,提交了10次才对,说明能力太渣了,得加油,加油!

本题我的迷糊点:1、题目的输出是一次实例一个输出,我刚开始将他一起输出,提交了好多次,后来运行了网上的结果,才知道,题目理解错了。

本题题目是:先输入一个n,紧接着n输入n个颜色,然后将输出颜色最多的那种颜色,注意如果有颜色一样的,输出排在前面的颜色

本题代码:

#include<iostream>
#include<map>
#include<string>
using namespace std;


int main()
{

map<string ,int>mapint;//定义map存放颜色统计
while(true)
{
map<string ,int>mapint;//定义map存放颜色统计
int count=0;//测试用例个数
cin>>count;
if(count>0&&count<=1000)
{
string colors;
for(int i=0;i<count;i++)
{
cin>>colors;
if(mapint.find(colors)==mapint.end())//查到最后也没发现,那就添加
{
mapint.insert(map<string ,int>::value_type(colors,1));
}
else//存在,那么数量增加1
{
mapint[colors] =mapint[colors]+1;
}
}
int cc=0;string color;
for( std::map<string, int>::iterator iter = mapint.begin();iter != mapint.end(); ++ iter )
{
if(cc<iter->second)
{
cc=iter->second;
color=iter->first;
}
}
cout << color <<endl;

}
else if(count==0)//退出
{
break;
}
}
return 0;
}

 

本代码中我使用了map,主要是我觉得可以动态往里面加键值对,而不是像数组那样需要定义固定大小的数组

但是map如何使用呢,我因为初学c++也不是太清楚,所以参照了网页http://blog.csdn.net/juiceda/article/details/7568342