题目大意是找出数组中出现次数超过一半的数。

基本思想:每遇到两个不同的数就消掉,设一个计数器就行了。

       存出现次数最大的那个数的出现次数。

     当下一个数与当前的数不同时,计数器减一,相同,则加一。

实现代码

 #include <iostream>
#include <stdio.h>
using namespace std; int main()
{
int n,x,m_max,cnt; // while(cin>>n)
while(scanf("%d",&n)!=EOF)
{ cnt = ;
// while(n--)
for(int i = ;i<n;i++)//相比之下,for的运行时间更少,所以能用for的,不要用while
{
// cin>>x;
scanf("%d",&x);
if(!cnt)
{
m_max = x;
cnt++;
}
else if(x!=m_max) cnt--;
else if(x==m_max) cnt++;
}
// cout<<m_max<<endl;
printf("%d\n",m_max);
}
return ;
}

1029:Ignatius and the Princess IV的更多相关文章

  1. 【HDU - 1029】Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV  先搬中文 Descriptions:   给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...

  2. 动态规划:Ignatius and the Princess IV

    #include<stdio.h> #include<string.h> #include<math.h> int main() { _int64 n,a; whi ...

  3. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  4. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  5. HDU 1029 Ignatius and the Princess IV (map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...

  6. HDOJ.1029 Ignatius and the Princess IV(map)

    Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...

  7. [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)

    Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32767K (Ja ...

  8. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  9. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

随机推荐

  1. (转)SSIS处理导入数据时, 存在的更新, 不存在的插入

    问题描述: 当你把数据从其他数据库, 或者是文本文件之类的其他数据源导入到目的数据库时, 有时希望在导入的处理中, 能够实现"数据存在时更新, 不存在时导入" 在之前, 一般是通过 ...

  2. (转)如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐)

    如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐) 2013-09-10 16:01 2152人阅读 ...

  3. Spring—Ioc

    IoC容器,最主要的就是完成对象的创建以及维护对象的依赖关系等. 所谓控制反转,包括两部分:一是控制,二是反转,就是把传统方式需要由代码来实现对象的创建.维护对象的依赖关系,反转给容器来帮忙管理和实现 ...

  4. mysql 分组统计SQL语句

    1.按照特定字段: 2.在某一日期范围内: 3.按日.按月统计: 4.动态传入数据库表名称. select <if test="dateType=="d"" ...

  5. python连接MySQL数据库问题

    Python 数据库图解流程 Connection.Cursor比喻 Connection()的参数列表 host,连接的数据库服务器主机名,默认为本地主机(localhost).user,连接数据库 ...

  6. opencv-ios开发笔记9 使用透视变换矫正扭曲的图片

    http://blog.csdn.net/baixiaozhe/article/details/51762086 摄像头观察一个矩形的图片时往往只能得到一个扭曲的图片: 原图: 实际情况是摄像头经常从 ...

  7. Python3基础 三元运算符 简单示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. ubuntu 18.04 64bit build tensorflow report error:C++ compilation of rule '//tensorflow/core/kernels:broadcast_to_op' failed (Exit 4)

    注意:笔者未能在Ubuntu18.04 64bit下成功从源码编译cpu版本的tensorflow Ans: 1.You are likely running out of memory. Try r ...

  9. iOS线程之——NSCondition

    多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美.这篇文章主要从线程创建与启动.线程的同步与锁.线程的交互.线 ...

  10. GitLab 使用指南(IntelliJ IDEA)

    一.环境 GitLab Community Edition 10.6.4 IntelliJ IDEA 2017.03 二.Git 使用 (Linux/MAC,cmd 模式) 本地新建项目(从Git服务 ...