返回目录
题意
给出两个递增序列,找出中位数。
样例(可复制)
4 11 12 13 14
5 9 10 15 16 17
样例输出
13
注意点
- 本题使用two pointers时,在数组最后添加了INF值,保证扫描过程中不会发生越界情况
- 本题输入输出请使用scanf和printf
- 本题还可以直接先合成一个新的数组,直接输出med位置的值
#include <bits/stdc++.h>
using namespace std;
const int INF=10000000;
int a[INF],b[INF];
int main(){
int n,m;
cin>>n;
for(int i=0;i<n;i++)scanf("%d",&a[i]);
cin>>m;
for(int i=0;i<m;i++)scanf("%d",&b[i]);
a[n]=b[m]=INF;
int med=(n+m-1)/2,count=0,i=0,j=0;
while(count<med){
if(a[i]<b[j])i++;
else j++;
count++;
}
cout<<min(a[i],b[j]);
return 0;
}