<题目链接>

题目大意:
给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个。

解题分析:

本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙。

解法一:

#include <cstdio>
int main()
{
int n,t,cnt,result;
while(scanf("%d",&n)!=EOF){
cnt=;
for(int i=;i<n;i++){
scanf("%d",&t);
if(cnt==){ //由于其它的所有数的出现次数都没它多,所以最后一定是这个特殊的数为result
cnt=;
result=t;
}
else{
if(t==result)cnt++;
else cnt--;
}
}
printf("%d\n",result);
}
return ;
}

map解法:

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std; int main(){
int n;
while(scanf("%d",&n)!=EOF){
map<int,int>mpa;
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
mpa[x]++;
}
map<int,int>::iterator it;
for(it=mpa.begin();it!=mpa.end();it++){
if(it->second>=(n+)/){
printf("%d\n",it->first);
break;
}
}
}
return ;
}

2018-09-26

HDU1029 Ignatius and the Princess IV (水题)的更多相关文章

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

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

  2. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

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

  3. [HDU1029]Ignatius and the Princess IV<桶 水题>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...

  4. HDU1029 - Ignatius and the Princess IV【水题】

    给你n个数字,请你找出出现至少(n+1)/2次的数字. 输入 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行 ...

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

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

  6. 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 ...

  7. (Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029

    Ignatius and the Princess IV 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 借鉴链接:https://blog.csd ...

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

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

  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. 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!

    作者:编辑部的故事   <  开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!   > 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐! Tutoria ...

  2. Confluence 6 导入一个文本文件

    Confluence 允许你从 Confluence 服务器上的一个目录中导入一个文本文件,然后将这个文本文件转换为 Confluence 的页面.每一个文本文件将会在 Confluence 中创建一 ...

  3. Linux端BaiduPCS-Go使用方法

    下载https://pan.baidu.com/s/1RFHTRE1c_JlP8rrZiERsTg 运行 ./BaiduPCS-Go 可能更新:update 登录:login 下载: d xxx 更多 ...

  4. 【python】多进程共享变量

    有一个字典变量,需要在多个进程间共享 使用Manager, 下面是一个小例子. 注意使用json前需要将类型转换. #!/usr/bin/python # coding=utf-8 import js ...

  5. Nginx详解十三:Nginx场景实践篇之防盗链

    防盗链: 目的:防止资源被盗用 防盗链设置思路 首要方式:区别哪些请求是非正常的用户请求 基于http_refer防盗链配置模块(判断refer(上一步的链接)信息是否为允许访问的网站) 配置语法:v ...

  6. Docker相关释义

    Docker相关释义 基础网站:http://www.runoob.com/docker/docker-tutorial.html Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上, ...

  7. 该问题是需要导包!!!需要pom中添加依赖The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --><depend ...

  8. Redis的过期策略和内存淘汰机制

    过期策略 我们set key的时候,都可以给一个expire time,就是过期时间,指定这个key比如说只能存活1个小时,我们自己可以指定缓存到期就失效. 如果假设你设置一个一批key只能存活1个小 ...

  9. SpringBank 开发日志 使用maven构建dubbo服务的可执行jar包

    写这篇日志的时候,我已经完成了这个目标,并且中间经历了一次面试.现在回过头看,已经觉得印象不那么深刻了,果然还是一边思考,一边记录这样最好.但我还是严格要求自己,从新做了梳理,对相关配置进行了整理和说 ...

  10. php抽奖概率算法

    方法一: function get_rand($proArr) { $result = array(); foreach ($proArr as $key => $val) { $arr[$ke ...