(中位数)给定 n(n 为奇数且小于 1000)个整数,整数的范围在 0~m(0 < m < 231) 之间,请使用二分法求这 n 个整数的中位数。所谓中位数,是指将这 n 个数排序之后, 排在正中间的数。
#include <iostream> using namespace std; const int MAXN = 1000; int n, i, lbound, rbound, mid, m, count; int x[MAXN]; int main() { cin >> n >> m; for (i = 0; i < n; i++) cin >> x[i]; lbound = 0; rbound = m; while ( (1) ) { mid = (lbound + rbound) / 2; (2) ; for (i = 0; i < n; i++) if( (3) ) (4) ; if (count > n / 2) lbound = mid + 1; else (5) ; } cout << rbound << endl; return 0; }