9度oj 题目1004:Median【排序水题】

来源:互联网 发布:qrcode.js免费下载 编辑:程序博客网 时间:2024/06/10 04:05
题目1004:Median

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:12541

解决:3434

题目描述:

    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 145 9 10 15 16 17
样例输出:
13
来源:
2011年浙江大学计算机及软件工程研究生机试真题
题意:给出2个数组,合并后求中位数,如果是偶数个,输出中间2个中的第一个。
import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);while(cin.hasNext()){int n,m,i;long[] a = new long[2000100];n = cin.nextInt();for (i = 0; i < n; i++){a[i] = cin.nextLong();}m = cin.nextInt();n+=m;for (; i < n; i++){a[i] = cin.nextLong();}Arrays.sort(a,0,n);System.out.println(a[(n-1)/2]);}cin.close();}}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝起热痱子了怎么办 宝宝热的起痱子怎么办 吃了有农药的菜怎么办 水银吃到肚子里怎么办 牙齿有牙结石怎么办可以去除 刷牙牙结石掉了怎么办 痘痘留下的黑印怎么办 脸上用了激素药怎么办 激素药膏用多了怎么办 8岁儿童牙齿不齐怎么办 两个牙齿之间有缝隙怎么办 蛀牙就剩牙根了怎么办 牙龈发白像烂了怎么办 3岁宝宝牙龈肿痛怎么办 1岁宝宝牙龈肿痛怎么办 牙疼引起的发烧怎么办 2岁小儿牙龈红肿怎么办 宝宝出牙牙龈红肿怎么办 3岁宝宝牙龈红肿怎么办 1岁宝宝牙龈红肿怎么办 4岁宝宝牙龈红肿怎么办 儿童牙黑了怎么办啊 牙黑了掉了一块怎么办 1岁幼儿牙齿腐蚀怎么办 我的大牙变黑了怎么办 牙齿里面黑了疼怎么办 最里面的牙黑了怎么办 牙龈的肉裂开了怎么办 有蛀牙怎么办可以变好么 拔智齿没拔干净怎么办 蛀牙只剩牙根了怎么办 牙掉的就剩牙根怎么办 蛀牙牙冠都掉了怎么办 拔智齿断了牙根怎么办 牙肉肿痛有脓包怎么办 种植牙到寿命后怎么办 牙齿松动快掉了怎么办 一颗牙齿松动了怎么办 种植牙牙冠掉了怎么办 牙就剩下牙根了怎么办 后面的大牙掉了怎么办