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 白名单允许进入和禁用
允许进入 启用 CORS 请求,将会从特定的 URL 来源中允许进入(Allow Incoming).URL 格式必须匹配格式 scheme://host[:port],结尾是没有反斜杠的(:port ...
- 巧用&&和|| 让逻辑代码更简洁,逼格看起来更高一点(玩笑脸)
通常当我们有一个需求 需要用到很多if else 进行条件筛选,例如: let level = 0; if(score > 12){ level = 4; } else if(score > ...
- CSS在线字体库,外部字体的引用方法@font-face
@font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...
- nginx之访问控制http_access_module与http_auth_basic_module
http_access_module 作用 基于IP的访问控制 语法 使用 局限性 解决办法 1. http_x_forwarded_for http_auth_basic_module 作用 基于用 ...
- linux cp命令使用
功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录, ...
- laravel 框架后台主菜单接口
后台菜单调用接口:/admin/manages ManageRepository类: 每个路由中注册: 等等: 最后后台菜单返回:
- jenkins持续集成:构建多个job同时执行
在jenkins 构建任务时,同时只能构建2个,但是有时候可能涉及到需要同时执行多个任务(大于2个),如果不能同时运行的话,就需要等待上一个执行完了,再执行第三个 比如用例非常多,需要把不同的用例分给 ...
- centos7.5上一步步部署jumpserver
1.基础环境:centos7.5:关闭防火墙和selinux 2.修改字符集,否则可能会报input/output error 的问题,因为日志里打印了中文 [root@xzw ~]# localed ...
- MongoDB数据库备份与还原、单表的导入导出
-------------------MongoDB备份与恢复------------------- 1.MongoDB数据库备份 1.语法: mongodump -h dbh ...
- 微信小程序开发 如何退出当前页面
默认是在首页 wx.navigateBack({ delta: -1 }); 详情参考. https://mp.weixin.qq.com/debug/wxadoc/dev/api/u ...