题目1004:Median

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:17536

解决:4860

题目描述:                       

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>
#define MAX 1000002
long int A[MAX];
long int B[MAX];
int main() {
int M,N;
while(scanf("%d",&M) != EOF) {
for(int i = ; i < M; i++) {
scanf("%ld",&A[i]);
}
scanf("%d",&N);
for(int i = ; i < N; i++) {
scanf("%ld",&B[i]);
}
int middle = (M+N-)/;
int a = , b =, i = ;
int flag = ;
while(i <= middle) {
if(A[a] < B[b] && a < M && b < N) {
a++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(A[a] > B[b] && a < M && b < N) {
b++;
i++;
flag = ;
//printf("%d\n",B[b]);
continue;
}
else if(a < M && b < N){
a++;
b++;
i++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(a < M && b >= N) {
a++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(a >= M && b < N) {
b++;
i++;
flag = ;
//printf("%d\n",B[b]);
continue;
}
}
if(flag == ) {
printf("%ld\n", A[a-]);
}
else {
printf("%ld\n", B[b-]);
}
}
return ;
}

九度oj 1004的更多相关文章

  1. 九度oj 1004 Median 2011年浙江大学计算机及软件工程研究生机试真题

    题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14162 解决:3887 题目描述: Given an increasing sequence S of N i ...

  2. 九度OJ 1004:Median

    #include <stdio.h> #include <stdlib.h> #include <limits.h> #define N 1000000 int a ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 前端APP打包管理规范

    1.包命名规范1)说明打包人:姓名拼音首字母小写 dev:开发环境 test:测试环境 pre:预发布环境 prod:正式环境 例如:版本号:1.2.3,说明:第一个数字大版本:变更框架.调整结构时变 ...

  2. 洛谷 P2691 逃离

    题目描述 一个n×n栅格是由n行和n列顶点组成的一个无向图,如图所示.用(i,j)表示处于第i行第j列的顶点.除了边界顶点(即满足i=1,i=n,j=1或j=n的顶点(i,j)),栅格中的所有其他顶点 ...

  3. windows10下安装TensorFlow Object Detection API

    由于官方的文件都是在乌班图和mac系统下的,就上网搜了以下. 按照下边这个博客安装成功了 https://blog.csdn.net/qq_28019591/article/details/82023 ...

  4. git 作成

    Git global setup git config --global user.name "高 文龍" git config --global user.email " ...

  5. 【转】iOS学习笔记(十五)——数据库操作(SQLite)

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的 ...

  6. java ArrayList remove 2 及正确方法

    https://www.cnblogs.com/chrischennx/p/9610853.html 正确方式 方法一,还是fori,位置前挪了减回去就行了, remove后i--: public v ...

  7. js实现23种设计模式(收藏)

    js实现23种设计模式 最近在学习面向对象的23种设计模式,使用java 和 javascript 实现了一遍,但是因为目前大三,还没有比较正规的大项目经验,所以学习的过程种我觉得如果没有一定的项目经 ...

  8. C# 替换去除HTML标记方法(正则表达式)

    [from] http://blog.csdn.net/sgear/article/details/6263848/// <summary> /// 将所有HTML标签替换成"& ...

  9. EXC_BAD_ACCESS调试

    有时候,你会遇到由EXCBADACCESS造成崩溃. 这篇文件会告诉你什么是EXCBADACCESS,以及它产生的原因.我还会提供一些EXCBADACCESS错误的解决方案. 1. 什么是 EXCBA ...

  10. 02Qt信号与槽(1)

    信号与槽 1.概述 ​ 信号和槽机制是 Qt 的核心机制,信号和槽是一种高级接口,应用于对象之间的通信,它是 Qt 的核心特性,也是 Qt 区别于其他工具包的重要地方.信号和槽是 Qt 自行定义的一种 ...