题目描写叙述:

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. C++虚析构函数的使用

    如果,你设计的程序里,释放对象实例的时候,有“使用某个基类的指针,来释放它指向的派生类的实例”这种用法出现的话,那么,这个基类的destructor就应该设计成virtual的. 如果,基类不是vir ...

  2. CreateProcess Access violation(越界访问)

    https://stackoverflow.com/questions/11339186/createprocess-fails-with-an-access-violation My aim is ...

  3. 安卓adb在拨号键盘上输入井号(#)

    安卓系统下由于#号是属于内定字符,需要转义为%23第一种方式:adb shell service call phone 1 s16 "%23"第二种方式:adb shell am ...

  4. JavaScript递归简单实现个对象深拷贝

    JavaScript中对象的深拷贝来说一直都算比较恶心 毕竟没有什么api能直接全拷贝了 得自己便利写  最近在项目中需要深拷贝 自己简单封了个方法 话不多说 直接上码 <!DOCTYPE ht ...

  5. CF528D Fuzzy Search 字符串匹配+FFT

    题意: DNA序列,在母串s中匹配模式串t,对于s中每个位置i,只要s[i-k]到s[i+k]中有c就认为匹配了c.求有多少个位置匹配了t. 分析: 这个字符串匹配的方式,什么kmp,各种自动机都不灵 ...

  6. sql数据表的设计思路

    好的表结构分的比较细致,个人理解大概主要分为主表.明细.历史记录表.中间表,辅助表结构应该分为:类型表.状态表.统计表.统计明细表等.为了一个功能加那么多表实在是多余,如果写一个非常复杂的业务逻辑还是 ...

  7. FreeMarker与SSH项目整合流程

    FreeMarker与SSH项目整合流程 学习了SSH之后,一般为了减少数据库的压力,会使用FreeMarker来生成静态HTML页面.下面简单说一下FreeMarker与SSH项目的整合全过程~ 前 ...

  8. 【Mysql数据库】知识点总结

    本文转载自:http://www.cnblogs.com/tonghun/p/7191131.html 一 数据库常用操作 mysql -u+username -p+password:登陆数据库管理系 ...

  9. CSS--基础块级元素与内联元素

    在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素.在HTML和XHTML中,块级元素不能继承自行内元素(即不能嵌套在行内元素),<p&g ...

  10. playbacktask

    / ** 播放应用程序的头文件. 此文件是头文件,用于定义Playback应用程序的API和数据类型. @file PlaybackTsk.h @ingroup mIAPPPlay @note什么都没 ...