Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 73297 Accepted Submission(s): 27379
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
5 green red blue red red 3 pink orange pink 0
red pink
学习map容器的使用。map容器的基础题,没什么好说的。代码如下:
#include <iostream>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
int n,maxn;
string maxc;
//char* a;
string a;
map<string, int> bollon;
int main()
{
while(scanf("%d",&n)&&n!=0)
{
int i;
bollon.clear();//多次输入,使用前注意清空bollon
for(i=0;i<n;i++)
{
//scanf("%s",a);
cin>>a;
bollon[a]++;
}
maxn=0;
map<string ,int>::iterator ite;
for(ite=bollon.begin();ite!=bollon.end();ite++)
if(ite->second>maxn)
{
maxn=ite->second;
maxc=ite->first;
}
cout<<maxc<<endl;
}
return 0;
}
附录:关于map容器的一些知识:
C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。
一、map的说明
1 头文件
#include <map>
2 定义
map<string, int> my_Map;
或者是typedef map<string, int> MY_MAP;
MY_MAP my_Map;
3 插入数据
(1) my_Map["a"] = 1;
(2) my_Map.insert(map<string, int>::value_type("b",2));
(3) my_Map.insert(pair<string,int>("c",3));
(4) my_Map.insert(make_pair<string,int>("d",4));
4 查找数据和修改数据
(1) int i = my_Map["a"];
my_Map["a"] = i;
(2) MY_MAP::iterator my_Itr;
my_Itr.find("b");
int j = my_Itr->second;
my_Itr->second = j;
不过注意,键本身是不能被修改的,除非删除。
5 删除数据
(1) my_Map.erase(my_Itr);
(2) my_Map.erase("c");
还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。
6 迭代数据
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}
7 其它方法
my_Map.size() 返回元素数目
my_Map.empty() 判断是否为空
my_Map.clear() 清空所有元素
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等
更高级的应用查帮助去吧,^_^;
979

被折叠的 条评论
为什么被折叠?



