九度OJ1004 Median
- 题目描写叙述:
 - 
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的更多相关文章
- 九度oj  题目1087:约数的个数
		
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
 - 九度OJ  1502  最大值最小化(JAVA)
		
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
 - 九度OJ,题目1089:数字反转
		
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
 - 九度 Online Judge 之《剑指 Offer》一书相关题目解答
		
前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...
 - 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
		
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
 - 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
		
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
 - 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
		
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
 - 九度OJ 1371 最小的K个数 -- 堆排序
		
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
 - 九度OJ 题目1384:二维数组中的查找
		
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
 
随机推荐
- postman使用方法详解
			
postman的使用方法详解 Collections:在Postman中,Collection类似文件夹,可以把同一个项目的请求放在一个Collection里方便管理和分享,Collection里 ...
 - 最小生成树 Prim算法 Kruskal算法实现
			
最小生成树定义 最小生成树是一副连通加权无向图中一棵权值最小的生成树. 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即,而 w(u, v) 代表此边的 ...
 - POJ-1011(sticks,深搜)
			
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
 - ArrayList集合(JDK1.8)
			
简述 List是继承于Collection接口,除了Collection通用的方法以外,扩展了部分只属于List的方法. 常用子类 ?ArrayList介绍 1.数据结构 其底层的数据结构是数组,数 ...
 - linux破解root登录密码,并重置
			
重启系统后按'e'键,进入编辑模式,在'UTF -8'后空格输入'rd.break'后,按快捷键'Ctrl+X'进入新界面进行编辑,代码如下: switch_root:/# mount -o remo ...
 - centos6 下FastDFS 在storage节点上nginx的fastdfs-nginx-module 模块编译出现的问题
			
centos6.6 下FastDFS 在storage节点上 make(编译)nginx的fastdfs-nginx-module 出现如下报错: /root/fastdfs-nginx-mo ...
 - Django框架基础知识12-中间件及上下文处理器
			
Django中间件(Middleware) 是一个轻量级.底层的“插件”系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出. django 中的中间件(middleware) ...
 - Haproxy的安装与配置
			
一.Haproxy概念 Haproxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.Haproxy特别适用于那些负载特大的web站点,这 ...
 - 了解DOM
			
DOM是为了方便处理层次型文档(如XML.HTML)的一种技术.DOM还提供了一套API,使开发人员可以用面向对象的方式来处理这些文档.对于XML文档来说,有专门的处理XML文档是XML DOM,一 ...
 - 【01】在 Github 上编辑代码
			
[01]在 Github 上编辑代码 当你使用 GitHub,看一些文件(任何的文本文件或者仓库),能看到一个顶部右侧有一个小铅笔图标.点击即可编辑文档. 完成后,按照提示点击「Propose fil ...