题目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. Python+selenium之测试报告(1)

    一.下载HTMLTestRunner.py HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展.它生成易于使用的 HTML 测试报告.HTMLTestRunne ...

  2. Django form组件应用

    form 组件的使用 class Register(forms.Form): user = forms.CharField(min_length=2, widget=widgets.TextInput ...

  3. Vue-Quill-Editor 修改配置,和图片上传

    1.富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改配置. 创建一个quill-config文件 /*富文本编辑图片上传配置*/ /*富文本编辑图片上传 ...

  4. linux - mysql 安装教程

    环境介绍>>>>>>>>>>>>>>>>>> 操作系统:Centos 7 mysql数据库版 ...

  5. vim 自动补全 颜色设置

    vim 自动补全 颜色设置 hi Pmenu ctermfg=black ctermbg=gray guibg=# hi PmenuSel ctermfg= ctermbg= guibg=# guif ...

  6. MySQL中同时存在创建和更新时间戳字段解决方法浅析

    MySQL中同时存在创建和更新时间戳字段解决方法浅析 明确我的MySQL版本.mysql> SELECT VERSION();+------------+| VERSION() |+------ ...

  7. Golang TCP转发到指定地址

    Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...

  8. iPhone如何设置自定义铃声?无需连接电脑,轻松几步就搞定!

    转载自: https://baijiahao.baidu.com/s?id=1594988016778457969&wfr=spider&for=pc 受够了iPhone自带的千篇一律 ...

  9. 封装一个优雅的element ui表格组件

    现在做后台系统用vue + elementUI 的越来越多,那element ui的 el-table 组件肯定也离不开.虽然element ui的table组件很好.但是表格和分页是分离的.每次写表 ...

  10. 数据库储存session信息代码

    今天给大家上一段代码,数据库存储session信息,你只需要将下面这段代码放到session文件中,然后再session_start()的地方引入sessiong文件就行啦,当然你就不用再写sessi ...