Jiudu -- question 1004: Median

Source: Internet
Author: User
Description:

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.

Input:

Each input file may contain in 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.

Output:

For each test case you shoshould output the median of the two given sequences in a line.

Sample input:
4 11 12 13 145 9 10 15 16 17
Sample output:
13
Import Java. io. bufferedinputstream; import Java. util. extends; public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {using CIN = new using (New bufferedinputstream (system. in); int num1 [], num2 [], mid, Count, median = 0, I, j, N, count1, count2; while (CIN. hasnext () {count1 = cin. nextint (); num1 = new int [count1]; for (int K = 0; k <count1; k ++) {num1 [k] = cin. nextin T ();} count2 = cin. nextint (); num2 = new int [count2]; for (int K = 0; k <count2; k ++) {num2 [k] = cin. nextint () ;}// start to calculate COUNT = count1 + count2; If (count % 2 )! = 0) {mid = (count/2) + 1;} else {mid = (count/2);} for (I = 0, j = 0, n = 1; (I <num1.length) & (j <num2.length) & (n <= mid); N ++) {If (num1 [I] <num2 [J]) {If (n = mid) {Median = num1 [I];} I ++;} else {If (n = mid) {Median = num2 [J];} j ++ ;}}if (n <= mid) {for (; I <num1.length; I ++, N ++) {If (n = mid) {Median = num1 [I] ;}}for (; j <num2.length; j ++, N ++) {If (n = mid) {Median = num2 [J] ;}} system. out. println (median );}}} /*************************************** * *********************** problem: 1004 User: Recall, Zhan language: Java result: accepted time: 200 MS memory: 18528 kb ************************************** **************************/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.