九度——題目1004:Median

來源:互聯網
上載者:User
題目描述:

    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
import java.io.BufferedInputStream;import java.util.Scanner; public class Main{    /**     * @param args     */    public static void main(String[] args)    {        Scanner cin = new Scanner(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.nextInt();            }            count2 = cin.nextInt();            num2 = new int[count2];            for (int k = 0; k < count2; k++)            {                num2[k] = cin.nextInt();            }            // 開始計算            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: 憶、瞻    Language: Java    Result: Accepted    Time:200 ms    Memory:18528 kb****************************************************************/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.