题目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. 抓取GridView "编辑"模式下,TextBox修改后的数值

    [FAQ]抓取GridView "编辑"模式下,TextBox修改后的数值 -- ASP.NET专题实务「上集」Ch.10 抓取GridView "编辑"模式下 ...

  2. Array - Container With Most Water

    /** * 此为暴力解法 * Find two lines, which together with x-axis forms a container, such that the container ...

  3. 用python Image读图

    https://www.cnblogs.com/kongzhagen/p/6295925.html import os name = [] with open('/media/hdc/xing/Dee ...

  4. 剑指offer24 二叉搜索树的后序遍历序列

    自己写的更简洁的代码 class Solution { public: bool VerifySquenceOfBST(vector<int> sequence) { int length ...

  5. MySQL 外键 表的查询

    自增补充 这是查看怎么创建的表, \G示旋转90度显示表的内容 表的自增的关键是** AUTO_INCREMENT=3**,在表中添加数据后,这个会自动改变,通过alert可以改变这个默认值 mysq ...

  6. 获得函数返回值类型、参数tuple、成员函数指针中的对象类型

    //function_traits.h,获得函数返回值类型.参数tuple.成员函数指针中的对象类型 //参考https://github.com/qicosmos/cosmos/blob/maste ...

  7. 【Git版本控制】GitHub上fork项目和clone项目的区别

    fork:在github页面,点击fork按钮,将别人的仓库复制一份到自己的仓库. clone:直接将github中的仓库克隆到自己本地电脑中 问题1:pull request的作用 比如在仓库的主人 ...

  8. 怎样处理jmeter中文乱码

    jmeter返回 中文乱码: 1.在jmeter的bin目录下,找到jmeter的配置文件,jmeter.properties,然后把 sampleresult.default.encoding=UT ...

  9. ccf 201803-1 跳一跳(Python实现)

    一.原题 问题描述 试题编号: 201803-1 试题名称: 跳一跳 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化 ...

  10. 谷歌放弃“不作恶” Alphabet要“遵守法律互相尊重”

    对于一些谷歌粉而言,谷歌那条“不作恶(Don’t be evil)”的行为准则是他们引以为傲的精神信仰.这一准则于1999年被首次确认,谷歌在2004年申请上市时也提到了这一点.不过现在这一点要改变了 ...