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 ( ...
随机推荐
- 24UDP通信
使用Qt提供的QUdpSocket进行UDP通信.在UDP方式下,客户端并不与服务器建立连接,它只负责调用发送函数向服务器发送数据.类似的服务器也不从客户端接收连接,只负责调用接收函数,等待来自客户端 ...
- Es+kafka搭建日志存储查询系统(设计)
现在使用的比较常用的日志分析系统有Splunk和Elk,Splunk功能齐全,处理能力强,但是是商用项目,而且收费高.Elk则是Splunk项目的一个开源实现,Elk是ElasticSearch(Es ...
- iframe 跨域问题解决方案 利用window.name+iframe跨域获取数据详解
详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...
- Hash课堂测试补写
Hash课堂测试补写 测试要求: 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,6 ...
- numpy数组广播
numpy数组的广播功能强大,但是也同时让人疑惑不解,现在让我们来谈谈其中的原理. 广播原则: 如果两个数组的后缘维度(即:从末尾开始算起的维度)的轴长相符或其中一方的长度为1,则认为它们是广播兼容的 ...
- POJ 1797 Heavy Transportation(最短路&Dijkstra变体)题解
题意:给你所有道路的载重,找出从1走到n的所有路径中载重最大的,即路径最小值的最大值. 思路:和之前的POJ3268很像.我们用Dijkstra,在每次查找时,我们把最大的先拿出来,因为最大的不影响最 ...
- 使用Python登陆QQ邮箱发送垃圾邮件 简单实现
参考:Python爱好者 知乎文章 需要做的是: 1.邮箱开启SMTP功能 2.获取授权码 上述两步百度都有. 源码: #!/usr/bin/env python from email.mime.te ...
- C# .NET 开发心得
1. 工作路径问题 1. 多项目构成的解决方案,Web APP作为启动项目时的工作路径 //当前执行的exe文件名 //C:\\Program Files\\IIS Express\\iisexpre ...
- HDU 4183 Pahom on Water(最大流)
https://vjudge.net/problem/HDU-4183 题意: 这道题目的英文实在是很难理解啊. 给出n个圆,每个圆有频率,x.y轴和半径r4个属性,每次将频率为400的圆作为起点,频 ...
- 流行得前端构建工具比较,以及gulp配置
前端现在三足鼎立的构建工具(不算比较老的ant,yeoman),非fis,grunt,gulp莫属了. fis用起来最简单,我打算自己得项目中使用一下fis. 先说一下gulp安装吧. 第一步:安装n ...