赞
踩
我自己的模板
- int l=0,r=100,ans=0;
- while(l<=r)
- {
- mid=(l+r)/2;
- if (check(mid)) {l=mid+1;ans = mid; }else r=mid-1; //注意+1,-1否则死循环
- }
- int main(){
- == key
- int m;
- while ( l <= r ) {
- m = ( l + r ) >> 1;
- if ( x[m] == key ) return m;
- else if ( key > x[m] )
- l = m + 1;
- else
- r = m - 1;
- }
-
- >= key 中最小的
- int ret;
- while ( l <= r ) {
- m = ( l + r ) >> 1;
- if ( x[m] >= key ) {
- ret = m;
- r = m - 1;
- } else
- l = m + 1;
- }
-
- <= key 中最大的
- int ret;
- while ( l <= r ) {
- m = ( l + r ) >> 1;
- if ( x[m] <= key ) {
- ret = m;
- l = m + 1;
- } else
- r = m - 1;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。