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

#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;
}

你可能感兴趣的:(hdu 1029 Ignatius and the Princess IV //map的使用)