HDU 1029 [Ignatius and the Princess IV]STL map应用

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029

题目大意:找出一串数字序列(长度为奇数)中出现次数>=(N+1)/2的数字并输出。

关键思想:map统计+遍历

代码如下:

//map统计+遍历 注意迭代器
#include<iostream>
#include <map>
using namespace std;
map<int,int>m;

int main(){
	int N;
	int temp;
	while(cin>>N){
		m.clear();
		for(int i=0;i<N;i++){
			scanf("%d",&temp); 
			m[temp]++;
		}
		for (map<int,int>::iterator ltr = m.begin();ltr != m.end(); ltr++){
        	if (ltr->second >= (N+1)/2){
          	 	cout<<ltr->first<<endl;
          	 	break;
          	}
    	}
	}
	return 0;
}

  

posted @ 2017-02-17 16:45  哇咔咔咔  阅读(99)  评论(0编辑  收藏  举报