'commit'
This commit is contained in:
23
TangDou/AcWing/low_bounde_test.cpp
Normal file
23
TangDou/AcWing/low_bounde_test.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int su = 5;
|
||||
int up[su] = {10, 8, 6, 5, 3};
|
||||
|
||||
int main() {
|
||||
//目标:找数组中比目标值小、最左侧的一个数字
|
||||
int x = 4;
|
||||
int k;
|
||||
for (k = 0; k < su; k++) if (up[k] < x) break;
|
||||
if (k == su) cout << "找不到!" << endl;
|
||||
else cout << k << endl;
|
||||
|
||||
int l = 0, r = su - 1;
|
||||
while (l < r) {
|
||||
int mid = (l + r) >> 1;
|
||||
if (up[mid] < x) r = mid;
|
||||
else l = mid + 1;
|
||||
}
|
||||
cout << l << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user