题目描写叙述:

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 14
5 9 10 15 16 17
例子输出:
13

代码

#include <stdio.h>
#include <stdlib.h> int main()
{
long n1,n2,index;
long * array1;
long * array2;
long i;
while(scanf("%ld",&n1) != EOF)
{
array1 = (long*)malloc(sizeof(long)*n1);
for(i = 0;i < n1;i++)
scanf("%ld",&array1[i]); scanf("%ld",&n2);
array2 = (long*)malloc(sizeof(long)*n2);
for(i = 0;i < n2;i++)
scanf("%ld",&array2[i]); index = n1 + n2;
if(index%2 == 0)
index = index / 2;
else
index = (index / 2) + 1; long count = 0;
long p1 = 0;
long p2 = 0;
long median;
int flag = 0;
while(!(count == index || p1 == n1 || p2 == n2))
{
if(array1[p1] <= array2[p2])
{
median = array1[p1];
p1++;
count++;
}
else
{
median = array2[p2];
p2++;
count++;
//flag = 2;
}
} if(count == index)
printf("%ld\n",median);
else if(p1 != n1)
{
while(count != index)
{
median = array1[p1];
p1++;
count++;
}
if(count == index)
printf("%ld\n",median);
}
else if(p2 != n2)
{
while(count != index)
{
median = array2[p2];
p2++;
count++;
}
if(count == index)
printf("%ld\n",median);
}
}
return 0;
}



九度OJ1004 Median的更多相关文章

  1. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  2. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  3. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  4. 九度 Online Judge 之《剑指 Offer》一书相关题目解答

    前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...

  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  6. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  7. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  8. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  9. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

随机推荐

  1. Kubernetes 架构(下)【转】

    上一节我们讨论了 Kubernetes 架构 Master 上运行的服务,本节讨论 Node 节点. Node 是 Pod 运行的地方,Kubernetes 支持 Docker.rkt 等容器 Run ...

  2. DLL动态链接库的创建

    dll的创建主要有两种方法:一是使用 __declspec(dllexport) 创建dll,二是使用模块定义(.def)文件创建dll. 使用 __declspec(dllexport) 创建dll ...

  3. CPP-基础:文字常量区

    内存不可写 char* 先看一个例子 ///////////// //代码1 #include <string> main() { char *buf = "good morni ...

  4. 16.04 下修改 ssh 默认端口

    打开/etc/ssh/ssh_config,在Port指令下追加新的端口设置: Port 8888 即允许通过端口 8888 进行 ssh 访问. 打开/etc/ssh/sshd_config,进行同 ...

  5. python interview questions

    referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...

  6. CF633H Fibonacci-ish II

    题目描述 题解: 坑题搞了三天. 莫队+线段树. 还有一些和斐波那契数列有关的性质. 首先答案是$a_1f_1+a_2f_2+…+a_nf_n$, 考虑插进去一个元素对答案产生的影响. 比如插进去一个 ...

  7. 22. SCHEMA_PRIVILEGES

    22. SCHEMA_PRIVILEGES SCHEMA_PRIVILEGES表提供有关schema(数据库)特权的信息.它从mysql.db系统表中获取其值. SCHEMA_PRIVILEGES表有 ...

  8. Vmare虚拟机中的3种网络连接方式

    安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和VMnet8,其他的未安装(当然也可以手动安装其他的). 其中: VMnet1是host网卡,用于host方式连接网络的. VMnet8是NAT网 ...

  9. 微信小程序的坑之wx.miniProgram.postMessage

    工作中有个需求是小程序的网页在关闭的时候,需要回传给小程序一个参数 查阅小程序官方文档,有这样一个接口 wx.miniProgram.postMessage ,可以用来从网页向小程序发送消息,然后通过 ...

  10. git 项目相关

    工具篇:Sourcetree 和 Git Bash Sourcetree Git一款非常好用的可视化工具,方便管理项目.下载地址 https://www.sourcetreeapp.com/ Git ...