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

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

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

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

实现代码

 #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. 2017-2018 ACM-ICPC Latin American Regional Programming Contest Solution

    A - Arranging tiles 留坑. B - Buggy ICPC 题意:给出一个字符串,然后有两条规则,如果打出一个辅音字母,直接接在原字符串后面,如果打出一个元音字母,那么接在原来的字符 ...

  3. JAVA面试题整理(5)-数据库

    数据库 1.Oracle/mysql分页有什么优化 2.悲观锁.乐观锁 悲观锁(Pessimistic Concurrency Control,PCC):假定会发生并发冲突,屏蔽一切可能违反数据完整性 ...

  4. img = img1*mask + img2*(1-mask) How do that ?

    原文地址:http://answers.opencv.org/question/160599/img-img1mask-img21-mask-how-do-that/ 如何提高一个简单操作的速度?最后 ...

  5. 20145333 《网络对抗技术》 PC平台逆向破解

    20145333 <网络对抗技术> PC平台逆向破解 20145333 <网络对抗技术> PC平台逆向破解 Shellcode注入 基础知识 Shellcode实际是一段代码, ...

  6. 20145204 《Java程序设计》第6周学习总结

    20145204 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 数据流 I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据 ...

  7. 20145325张梓靖 《Java程序设计》第4周学习总结

    20145325张梓靖 <Java程序设计>第4周学习总结 教材学习内容总结 何谓继承 继承共同行为 继承基本上就是避免多个类间重复定义共同行为:可把相同的程序代码提升为父类:用关键字 e ...

  8. 2017-2018-1 JaWorld 团队作业--冲刺6

    2017-2018-1 JaWorld 团队作业--冲刺6(20162308) 实现 由于我在冲刺部分负责的是类之间的耦合,所以我就介绍一下本次游戏的总体实现. 我们定义了Sprite类,即精灵类,游 ...

  9. 从0开始学习 GITHUB 系列之「GIT 进阶」【转】

    本文转载自:http://stormzhang.com/github/2016/06/16/learn-github-from-zero5/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...

  10. django 常用字段类型

    <> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的最大字符数 ...