#include <iostream>
using namespace std;
bool check(int a) { return true; }
int main() {
//b_search
int l, r;
while (l < r) {
int mid = l + r + 1 >> 1;
if (check(mid))l = mid;
else r = mid - 1;
}
while (l < r) {
int mid = l + r >> 1;
if (check(mid))r = mid;
else l = mid + 1;
}
return 0;
}
2 comments
总结:1.l不能直接等于mid因为 0+1>>1=0,陷入死循环,所以搜索右边界的时候l+r+1>>1。2.else后l=mid+1和r=mid-1是为了使l最终等于r,打破循环。