题目描写叙述:

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. eclipse生成spring boot jar包

    1.右击项目,选择Run As - Maven clean 2.右击项目,选择Run As - Maven install 3.成功后 会在项目的target文件夹下生成jar包 4.将打包好的jar ...

  2. Markdown的安装和语法

    步骤: 1.打开webstorm,File-->Setting-->输入plugin-->Install JetBrains plugin-->输入markdown--> ...

  3. CPP-基础:模板

    // template.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include &l ...

  4. U盘制作安装盘后容量不能恢复的解决方案

    diskpartlist diskselect disk 0/1 --看具体U盘是0还是1clean

  5. \p{space}|\p{Hex}|\p{Digit}|转译符|\1|\g{-1}|[^ab]|/([^\d\D]+)/

    小骆驼 第七章 漫游正则表达式王国 #!/usr/bin/perl use strict; use warnings; $_ = 'ab \ cde f ghijk10.x12ln'; if(/d/) ...

  6. Mac 安装Fiddler 抓包工具

    官方安装文档https://www.telerik.com/download/fiddler/fiddler-osx-beta 如果输入mono Fiddler.exe报下面这个错误 WARNING: ...

  7. java+selenium+maven+IntelliJ IDEA 搭建简单的UI自动化测试环境

    1. 用IntelliJ IDEA新建一个maven工程 2. 在pom.xml中添加依赖: <dependency> <groupId>org.seleniumhq.sele ...

  8. Postfix mail for azengna.com loops back to myself -solve

    设置 /etc/postfix/main.cf 原配置 mydestination = $myhostname, localhost.$mydomain, localhost 改为 mydestina ...

  9. UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)

    题意: 分析: 欧拉通路:图连通:图中只有0个或2个度为奇数的结点 这题我们只需要判断选择的边构成多少个联通块, 再记录全部联通块一共有多少个奇度顶点. 然后我们在联通块中连线, 每次连接两个联通块就 ...

  10. django1.11 启动错误:Generator expression must be parenthesized

    错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...