简单的sort排序找中间数字不行,复杂度太高了,然后就要巧妙一点的方法,还有他题目没说要是偶数个取前面奇数位加一的数字,我以为取平均数。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<time.h>
using namespace std;
const int maxn = 1000001;
int a[maxn], b[maxn];
int main()
{
int n, m, index = 0;
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]);
}
int medianpos = (n + m - 1) / 2;
a[n] = b[m] = 10000000000001;
int i = 0, j = 0, count = 0;
while (count < medianpos)
{
if (a[i] < b[j]) i++;
else j++;
count++;
}
if (a[i] < b[j])
{
printf("%d
", a[i]);
}
else
{
printf("%d
", b[j]);
}
return 0;
}