zoukankan      html  css  js  c++  java
  • PAT 甲级1029 Median (25分)

    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 nondecreasing 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 Specification:

    Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤) 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 Specification:

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

    Sample Input:

    4 11 12 13 14
    5 9 10 15 16 17

    Sample Output:

    13   

    【AC】简单序列合并问题

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int n, m, a[500000], b[500000], c[500000];
     7     cin >> n;
     8     for(int i = 0; i < n; i++) scanf("%d", &a[i]);
     9     cin >> m;
    10     for(int i = 0; i < m; i++) scanf("%d", &b[i]);
    11     int j = 0,k = 0, index = 0;
    12     while(j < n && k < m)
    13     {
    14         if(a[j] <= b[k]) c[index++] = a[j++];
    15         else c[index++] = b[k++];
    16     }
    17     while(j < n) c[index++] = a[j++];
    18     while(k < m) c[index++] = b[k++];
    19     if( (m+n) % 2!=0) cout << c[(m+n) / 2];
    20     else cout << c[(m+n) / 2 - 1];
    21     return 0;
    22 }
  • 相关阅读:
    求100内的数和
    汉诺塔(印度传说)
    ORA-06502:PL/SQL:数字或值错误:数值精度太高
    触发器的编写
    mysql改密码
    LeetCode-238 Product of Array Except Self
    LeetCode-236 Lowest Common Ancestor of a Binary Tree
    LeetCode-233 Number of Digit One
    LeetCode-230 Kth Smallest Element in a BST
    LeetCode-229 Majority Element II
  • 原文地址:https://www.cnblogs.com/kamisamalz/p/13676327.html
Copyright © 2011-2022 走看看