1029. Median (25)

题目地址:http://www.patest.cn/contests/pat-a-practise/1029
// 就是合并 两个有序数组 的过程

/* http://www.patest.cn/contests/pat-a-practise/1029 1029. Median (25) */
#include <iostream> 
#include <cstdio> 
#include <deque> 

using namespace std;

int main()
{
    //freopen("in.txt", "r", stdin);
    int n1, n2, i;
    deque<long> s1, s2, s;
    long tmp;
    scanf("%d", &n1);
    for (i = 0; i < n1; i++){
        scanf("%ld", &tmp);
        s1.push_back(tmp);
    }
    scanf("%d", &n2);
    for (i = 0; i < n2; i++){
        scanf("%ld", &tmp);
        s2.push_back(tmp);
    }
    while (!s1.empty() && !s2.empty())
    {
        if (s1.front() < s2.front())
        {
            s.push_back(s1.front());
            s1.pop_front();
        }
        else if(s1.front() == s2.front())
        {
            s.push_back(s1.front());
            s1.pop_front();
            s.push_back(s2.front());
            s2.pop_front();
        }else{
            s.push_back(s2.front());
            s2.pop_front();
        }
    }
    // 最后最多只能由一个是有元素的
    while (!s2.empty()){
        s.push_back(s2.front());
        s2.pop_front();
    }
    while (!s1.empty()){
        s.push_back(s1.front());
        s1.pop_front();
    }
    cout << s[(s.size() - 1) / 2] << endl;
    return 0;
}

你可能感兴趣的:(1029. Median (25))