HDU1029 Ignatius and the Princess IV (水题)
<题目链接>
题目大意:
给你一段序列,问你在这个序列中出现次数至少为 (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 (水题)的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [HDU1029]Ignatius and the Princess IV<桶 水题>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...
- HDU1029 - Ignatius and the Princess IV【水题】
给你n个数字,请你找出出现至少(n+1)/2次的数字. 输入 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行 ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- 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 ...
- (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 ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
随机推荐
- Modbus库开发笔记之十:利用协议栈开发Mosbus RTU Slave应用
上一节我们使用协议占开发了一个Modbus TCP Server应用.接下来我们使用协议栈在开发一个基于串行链路的Mosbus RTU Slave应用. 根据前面对协议栈的封装,我们需要引用Modbu ...
- SoapUI、Jmeter、Postman三种接口测试工具的比较分析
前段时间忙于接口测试,也看了几款接口测试工具,简单从几个角度做了个比较,拿出来与诸位分享一下吧.各位如果要转载,请一定注明来源,最好在评论中告知博主一声,感谢.本报告从多个方面对接口测试的三款常用工具 ...
- Swift DispatchQueue
延迟2s执行 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2)
- Vuex状态管理模式的面试题及答案
转载:点击查看原文 1.vuex有哪几种属性? 答:有五种,分别是 State. Getter.Mutation .Action. Module 2.vuex的State特性是? 答: 一.Vuex就 ...
- Allegro PCB Design GXL (legacy) 将指定的层导出为DXF
Allegro PCB Design GXL (legacy) version 16.6-2015 1.菜单:Display > Color/Visibility... 2.打开Color Di ...
- linux把程序做成系统服务并自启动
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 一.chkconfig 的使用语法1.c ...
- Loadrunner11.0 录制手机App脚本的方法一
使用Loadrunner录制手机终端App脚本 1. 说明 目前手机APP上的功能日益丰富,对手机应用功能的性能测试需求也越来越多.公司比较抠门没有花钱买Loadrunner,可怜我们工作中一直用的破 ...
- office之Excel 你会用 Ctrl + E 吗?
从Excel97至现在的最新版本2016,如果评选Excel中最简单.易用.功能最强大的技巧,非2013版新增的“快速填充”(快捷键 Ctrl+E)莫属.向下拖动复制后,打开下拉菜单,就可以看到它的身 ...
- springboot快速使用
1.编写SpringConfig 用于实例化Spring容器 @Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件 @Bean // 通过该注解来表明是 ...
- TFS 生成任务报错:目录不是空的
转到代理目录下,将生成文件夹清空,重新启动生成任务即可