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 ( ...
随机推荐
- Confluence 6 服务器的许可证信息
Confluence 6 服务器的许可证信息. https://www.cwiki.us/display/CONFLUENCEWIKI/Managing+your+Confluence+License
- Confluence 6 自定义 Decorator 模板的宏和针对高级用户
宏 页面的某些部分使用的是 Velocity 宏进行创建的,包括导航栏.有关宏的创建,你可以参考页面 Working With Decorator Macros 页面中的内容. 针对高级用户 vel ...
- web的分页方法
web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...
- Intenet 地址
java.net.InetAddress类是java对Ip地址(包括ipv4和ipv6)的高层表示,大多数其他网络类都要用到这个类,包括Socket, ServerSocket, URL, Datag ...
- du命令
选项 例1:显示单个文件的大小(默认单位K) [root@zabbix alertscripts]# du -h sendim.py 4.0k sendim.py 例2:显示某个目录的总大小 例3:输 ...
- js--基础(对象、数组、函数、if语句、while语句、do while语句、continue语句、break语句)
三.流程控制:1.单行语句var age =20;//单行语句 2.复合语句花括号包含起来的与聚集和叫做复合语句,一对花括号表示一个复合语句 ,处理时可以当成一个单行语句来看待,一般复合句与叫做代码块 ...
- C++ Primer 笔记——嵌套类 局部类
1.嵌套类是一个独立的类,与外层类基本没什么关系.特别的是,外层类的对象和嵌套类的对象是相互独立的.在嵌套类的对象中不包含任何外层类定义的成员,在外层类的对象中也不包含任何嵌套类定义的成员. 2.嵌套 ...
- 微信小程序开发 如何退出当前页面
默认是在首页 wx.navigateBack({ delta: -1 }); 详情参考. https://mp.weixin.qq.com/debug/wxadoc/dev/api/u ...
- 眼底血管分割训练函数(SVM,Adaboost)
# -*- coding: utf-8 -*- import numpy as np from sklearn import svm from sklearn.model_selection impo ...
- Fisher–Yates shuffle 算法
费希尔 - 耶茨洗牌 维基百科,自由的百科全书 所述费-耶茨洗牌是一种算法,用于产生随机排列的有限的序列 -in平原而言,算法打乱的序列.该算法有效地将所有元素放在帽子里; 它通过随机从帽子中 ...