尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还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=0; i<na; i++) { scanf("%d", &a[i]); } scanf("%d", &nb); vector<int> b(nb); for (int i=0; i<nb; i++) { scanf("%d", &b[i]); } int ia = 0, ib = 0; int idx = 0; int mid = (na + nb) / 2; int mv = 0, 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) & 1) { // odd cout<<mv2<<endl; } else { // even cout<<mv<<endl; } system("pause"); return 0; }
log(na+nb)算法见:http://www.cnblogs.com/lailailai/p/3982103.html