问题链接:HDU1029 Ignatius and the Princess IV。基础练习题,用C++语言编写。

题意简述:输入n(n是奇数),然后输入n个整数,求出现(n+1)/2次的整数。

问题分析:n是奇数,(n+1)/2是n的一半以上,只要将n个数据排序,出现(n+1)/2次的整数必然会出现在中间位置。

本问题使用C++语言编写的原因是函数sort()的参数简单,使用方便。

AC的C++语言程序如下:

/* HDU1029 Ignatius and the Princess IV */#include <iostream>
#include <algorithm>using namespace std;const int MAXN = 999999;
int data[MAXN];int main()
{int n;while(cin >> n) {for(int i=0; i<n; i++)cin >> data[i];sort(data, data + n);printf("%d\n", data[(n + 1) / 2]);}return 0;
}

转载于:https://www.cnblogs.com/tigerisland/p/7564443.html

HDU1029 Ignatius and the Princess IV相关推荐

  1. hdu-1029 Ignatius and the Princess IV

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目类型: 给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是 ...

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

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

  3. HDU1029——Ignatius and the Princess IV

    题意:求N个数中出现次数大于等于(N+1)/2的数.因为N是奇数,所以也就是求出现次数一半以上的数. 定义变量ans=第一个数.time=1,向后循环如果和ans相等time++,否则time--,当 ...

  4. E - Ignatius and the Princess IV

    ####E - Ignatius and the Princess IV "OK, you are not too bad, em- But you can never pass the n ...

  5. Hdu 1029 Ignatius and the Princess IV

    思路:普通的cin会超时 方法很妙 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 ...

  6. HDU - 1029 Ignatius and the Princess IV

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=1029 题目大意 给你n(n为奇数, n < 1e6)个数,让你找到个数 >=( ...

  7. hdu1027 Ignatius and the Princess II (全排列 amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  8. LETTers比赛第三场 --1002 Ignatius and the Princess III解题报告

    提交人:侯建鹏 提交日期:2012/4/16 1002 Ignatius and the Princess III 解题思路(一):动态规划 先设置int p[N][N]; Dp 方程为:p[i][j ...

  9. HDU-1026 Ignatius and the Princess I (BFS)

    题目:HDU-1026 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目: Ignatius and the Princess I Time ...

  10. 【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)

    题干: The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pr ...

最新文章

  1. boost::sort模块实现相关的浮点排序测试
  2. nextcloud php 版本,Nextcloud版本升级教程
  3. 第43课 最大公约数 动动脑 第2题《小学生C++趣味编程》
  4. linux kvm服务,Linux-让Kvm虚拟机开放服务端口!
  5. 解读鸿蒙轻内核的监控器:异常钩子函数
  6. python学习笔记之property
  7. [Ext JS] 组件浮动一篇通(floating、alignTo)
  8. CCF NOI1028 判断互质
  9. 万能平板刷机软件_平板电脑怎么刷机 平板电脑刷机方法【教程】
  10. 好用的BUS调试工具-Bus Hound
  11. python面向对象练习题
  12. 8、共射放大电路一般性质与放大电路的直流偏移
  13. js initialize
  14. php php_sapi cli,php_sapi_name() cli
  15. 猫眼电影Top100爬取
  16. 如何使用kodi Mac安装中文插件
  17. Matlab画圆方向图,如何在matlab里画天线的立体方向图?
  18. 【分治算法】大整数乘法
  19. java画图板之平面山水画(一)
  20. 如何查看浏览器http请求协议

热门文章

  1. 中文编码--中国程序员之”银弹“?
  2. 网络数据传输过程分析
  3. Quartz job Cluster下报错
  4. [原]超快速搞定linux的vnc
  5. TC中编辑程序快捷键
  6. linux  指令 备注
  7. 网络爬虫的基本原理(一)
  8. 用VS调试 javascript
  9. 红帽企业版Linux 6安装指南(中文)
  10. 服了!程序员面试现场80行代码写俄罗斯方块,最终拿到15K月薪!