尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还tmd一直再算平均,卧槽
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector> using namespace std; int min(int a, int b) {
return a<b? a:b;
} int main() { int na, nb; scanf("%d", &na);
vector<int> a(na); for (int i=; i<na; i++) {
scanf("%d", &a[i]);
} scanf("%d", &nb);
vector<int> b(nb); for (int i=; i<nb; i++) {
scanf("%d", &b[i]);
} int ia = , ib = ; int idx = ;
int mid = (na + nb) / ; int mv = , mv2;
while (ia < na && ib < nb && idx < mid) {
if (a[ia] <= b[ib]) {
mv = a[ia];
ia++;
} else {
mv = b[ib];
ib++;
}
idx++;
} while (ia < na && idx < mid) {
mv = a[ia];
ia++;
idx++;
} while (ib < nb && idx < mid) {
mv = b[ia];
ib++;
idx++;
} if (ia < na && ib < nb) {
mv2 = min(a[ia], b[ib]);
} else if (ia < na) {
mv2 = a[ia];
} else if (ib < nb) {
mv2 = b[ib];
} if ((na + nb) & ) {
// odd
cout<<mv2<<endl;
} else {
// even
cout<<mv<<endl;
} system("pause");
return ;
}
log(na+nb)算法见:http://www.cnblogs.com/lailailai/p/3982103.html