C++ 二分查找法求中位数 - 代码填空练习
#include
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 (lbound <= rbound) { mid = (lbound + rbound) / 2; count = 0; for (i = 0; i < n; i++) if (x[i] <= mid) count++; if (count > n / 2) lbound = mid + 1; else rbound = mid - 1;
cout << mid << " " << lbound << " " << rbound << " " << count << endl;
}
cout << rbound << endl;
return (0);
}
原文地址: https://www.cveoy.top/t/topic/qu5o 著作权归作者所有。请勿转载和采集!