• 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 }
  • 相关阅读:
    linux下解压命令大全
    关于一个订单多商品如何发表评价的问题
    浏览器通知
    简易时钟
    微信小程序地图控件篇 自定义图标被地图覆盖的问题
    面向对象拖拽
    elementUI的Dialog弹出框蒙版被遮住
    面向对象轮播图
    高内聚低耦合
    苦逼程序员
  • 原文地址:https://www.cnblogs.com/kamisamalz/p/13676327.html
Copyright © 2020-2023  润新知