首页 > 其他 > 详细

hdu 1029 Ignatius and the Princess IV

时间:2017-03-15 14:39:21      阅读:194      评论:0      收藏:0      [点我收藏+]

题意:给n(n为奇数)个数,接下来给出n个数,并且其中一个数出现大于等于(n+1)/2次,请输出那个数

分析:数不多,可以map记录每个数的个数,然后迭代器寻找就可以了

      也可以充分利用数大于一半的条件

 

技术分享
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;


//记得leetcode 上有过这道题
//毕竟数大于一半,充分利用条件
//竟然才和map一样快,看来数不多
int main(){
    int n,m,cnt,x;
    while(~scanf("%d",&n)){
        cnt=0;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            if(cnt==0){
                cnt++;m=x;
            }
            else if(x==m)cnt++;
            else cnt--;
        }
        cout<<m<<endl;
    }
    return 0;
}
View Code

 

hdu 1029 Ignatius and the Princess IV

原文:http://www.cnblogs.com/jihe/p/6553790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!