(中位数)给定 n(n 为奇数且小于 1000)个整数,整数的范围在0~m(0 <m< 2^31)之间,请使用二分法求这 n 个整数的中位数。所谓中位数,是指将这 n 个数排序之后,排在正中间的数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #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(①) { mid=(lbound+rbound)/2; ②; for(i = 0; i < n; i++) { if(③) ④; } if(count > n/2) lbound = mid + 1; else ⑤; } cout << rbound << endl; return 0; } |
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1. 填空位置 ①
2. 填空位置 ②
3. 填空位置 ③
4. 填空位置 ④
5. 填空位置 ⑤