1029:Ignatius and the Princess IV
题目大意是找出数组中出现次数超过一半的数。
基本思想:每遇到两个不同的数就消掉,设一个计数器就行了。
存出现次数最大的那个数的出现次数。
当下一个数与当前的数不同时,计数器减一,相同,则加一。
实现代码:
#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的更多相关文章
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- 动态规划:Ignatius and the Princess IV
#include<stdio.h> #include<string.h> #include<math.h> int main() { _int64 n,a; whi ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 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 ...
- 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 ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- [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 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
随机推荐
- Hadoop mapreduce自定义排序WritableComparable
本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...
- Educational Codeforces Round 57 Solution
A. Find Divisible 签到. #include <bits/stdc++.h> using namespace std; int t, l, r; int main() { ...
- tomcat_日志打印格式问题
1.需要在Catalina/conf/server.xml中设置一下:将文件中这一段的注释去掉(如下),然后将pattern的值改为combined ,这个模式下记录的日志比较详细. ...
- Python 读取写入配置文件 ConfigParser
https://blog.csdn.net/piaodexin/article/details/77371343 https://www.cnblogs.com/feeland/p/4502931.h ...
- 通过代码来操作SQLite的示例
Getting started with SQLite in C# http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sq ...
- POJ 2112 Optimal Milking(二分+最大流)
http://poj.org/problem?id=2112 题意: 现在有K台挤奶器和C头奶牛,奶牛和挤奶器之间有距离,每台挤奶器每天最多为M头奶挤奶,现在要安排路程,使得C头奶牛所走的路程中的最大 ...
- python 读空的json文件
读空的json文件,python2和python3 的错误提示是不一样的 python2: ValueError: No JSON object could be decoded python3: j ...
- Eclipse.导出可执行的jar(包含第3方jar)
1.不包含 第三方jar 的情况: (1)项目右键--> Export... --> 选择"JAR file"(记得有看到有的博客上写的是选择"Runnabl ...
- 【Docker】Windows下docker环境搭建及解决使用非官方终端时的连接问题
背景 时常有容器方面的需求,经常构建调试导致测试环境有些混乱,所以想在本地构建一套环境,镜像调试稳定后再放到测试环境中. Windows Docker 安装 安装docker toolbox 博主的电 ...
- wpf中的触发器详解 (转自 乂乂的日志 - 网易博客)
2010-03-24 16:19:07| 分类: WPF相关 | 标签: |字号大中小 订阅 wpf中的触发器详解 WPF/C# 2009-08-24 11:32:50 7.1.2 简单 ...