问题网址:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2104
顺便温习了下map的用法,源代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;
map<string,int> ballon; //定义容器
map<string, int>::iterator iter,popular ; //定义迭代器
int main(){
int i, n;
string ba;
while (cin>>n && n!=0){
ballon.clear(); //清空容器用成员函数clear()
for (i = 0; i < n; i++){
cin >> ba;
iter = ballon.find(ba); //查找元素用成员函数find()返回的是迭代器,用count()返回的是0或1
if (iter != ballon.end()){ //find()查找不到元素则返回end()
iter->second++; //迭代器访问数据用->first,->second
}
else
ballon[ba] = 1; //数组方式插入数据,会覆盖掉;还可以用insert(pair<,>(,)),如存在则插入失败
}
popular = ballon.begin();
for (iter = ballon.begin(); iter != ballon.end(); iter++){ //最常用的迭代方式,还可用数组形式,1到size()
if (iter->second > popular->second)
popular = iter;
}
cout<<popular->first<<endl;
}
return 0;
}
//其他:erase(iter),empty(),重载<