1389 蓝桥杯 二分查找数组元素 简单
//C++风格解法1,lower_bound(),通过率100%
//利用二分查找的方法在有序的数组中查找,左闭右开
#include <bits/stdc++.h>
using namespace std;
int main(){
int data[200];
for(int i = 0 ; i < 200 ; ++i) data[i] = 4 * i + 6;
int tager; cin >> tager; //输入目标值
cout << lower_bound(data,data + 200,tager) - data;
return 0;
}
在有序数组中进行二分查找,升序,查找第一个 >= target的元素,时间复杂度O(logn)
lower_bound(data,data + 200,tager)返回物理地址,减去首地