您的位置:首页 > 其它

hdu 1029 Ignatius and the Princess IV //map的使用

2015-03-08 11:14 357 查看
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

int main()
{
int n, i, k;
map<int, int>a;

while(scanf("%d", &n) != -1){
for(i=0; i<n; i++){
scanf("%d", &k);
/* if(a.count(k)==0){
a.insert(pair<int, int>(k, 1)); //map的插入, count()是返回key出现的次数,在map里,key只能是0和1
}
else*/
a[k]++; //会自动判断是否存在key k,存在则++, 不存在则插入后初始化后++
}
map<int, int>::iterator pos;
for(pos=a.begin(); pos!=a.end(); pos++){
if(pos->second >= (n+1)/2){
printf("%d\n", pos->first);
break;
}
}
a.clear();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: