7-1 兩個有序序列的中位數(25 分)
阿新 • • 發佈:2019-01-06
已知有兩個等長的非降序序列S1, S2, 設計函式求S1與S2並集的中位數。有序序列A0 ,A1,⋯,AN−1的中位數指A(N−1)/2的值,即第⌊(N+1)/2⌋個數(A0為第1個數)。
輸入格式:
輸入分三行。第一行給出序列的公共長度N(0
5
1 3 5 7 9
2 3 4 5 6
輸出樣例1:
4
輸入樣例2:
6
-100 -10 1 1 1 1
-50 0 2 3 4 5
輸出樣例2:
1
程式碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> C;//兩序列的並集
vector<int>::iterator p;
int n,data1,data2;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>data1;
C.push_back(data1);
}
for(int i=0; i<n; i++)
{
cin>>data2;
C.push_back(data2);
}
sort(C.begin(),C.end());
cout <<C[n-1];
}