九度OJ —— 1004
来源:互联网 发布:手机分轨软件 编辑:程序博客网 时间:2024/05/21 09:31
题目描述:
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13. Given two increasing sequences of integers, you are asked to find their median.
输入:
Each input file may contain more than one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.
输出:
For each test case you should output the median of the two given sequences in a line.
样例输入:
4 11 12 13 145 9 10 15 16 17
样例输出:
13
#include <iostream>#include <algorithm> //sort()函数头文件 using namespace std;int a[2000000];int b[1000000];int main(){ int m,n,k; int i,j; while(cin>>m){ for(i=0;i<m;i++) cin>>a[i]; cin>>n; for(i=0;i<n;i++) cin>>b[i]; k=0; for(i=m;i<m+n;i++){ a[i]=b[k]; k++; } sort(a,a+m+n); if((m+n)%2) cout<<a[(m+n)/2]<<endl; else cout<<a[(m+n)/2-1]<<endl; } return 0;}
主要就用到了一个sort函数,将两个合起来的数列进行了排序,在选取中间的数字。
sort函数头文件是#include <algorithm>
阅读全文
0 0
- 九度OJ —— 1004
- 九度OJ 1004
- 九度 OJ 1004
- 九度OJ 1004
- 九度OJ 1004
- 九度OJ——1384
- 九度OJ —— 1000
- 九度OJ —— 1001
- 九度OJ —— 1002
- 九度OJ —— 1003
- 九度OJ —— 1005
- 九度OJ——1014排名
- 九度OJ——1172哈夫曼树
- 九度OJ——1201二叉排序树
- 九度OJ——1144Freckles
- 九度OJ—题目1032:ZOJ
- 九度OJ—题目1057:众数
- 九度OJ—题目1056:最大公约数
- 阿里云堡垒机操作
- Python中的字符串与字符编码:编码和转换问题
- JNI的知识总结(全)
- git命令将本地项目上传到github
- JS类,对象,实例,属性,方法,事件区别 以及 原生js click和 onclick的区别
- 九度OJ —— 1004
- PHP安装扩展
- 代公众号调用接口
- SharePoint关于publish page, WiKi page, Web part page区别
- UVALive5713[Qin Shi Huang's National Road System] 枚举+每对节点间最小瓶颈路
- 信道估计算法
- CSS居中及布局
- Android OpenGL ES 绘图 -- 材质渲染
- CmakeList相关