标签:style blog color sp strong on 2014 log as
二分查找:
1:大于等于xx的第一个数
int bin_s(int xx) { int l=1,r=10; int ans=-1; while(l<=r){ int m=(l+r)>>1; if(a[m]>=xx) ans=m,r=m-1; //!!! else l=m+1; } return ans; } e.g: int a[]={0,1,2,5,5,5,5,7,8,9,10}; in: 5 out: 32:大于xx的第一个数
int bin_s(int xx) { int l=1,r=10; int ans=-1; while(l<=r){ int m=(l+r)>>1; if(a[m]>xx) ans=m,r=m-1; //!!! else l=m+1; } return ans; } e.g: int a[]={0,1,2,5,5,5,5,7,8,9,10}; <pre name="code" class="cpp">in: 5 out: 73:小于等于xx的第一个数(接近xx)
int bin_s(int xx) { int l=1,r=10; int ans=-1; while(l<=r){ int m=(l+r)>>1; if(a[m]>xx) r=m-1; else ans=m,l=m+1; } return ans; } e.g: int a[]={0,1,2,5,5,5,5,7,8,9,10}; <pre name="code" class="cpp">in: 5 out: 64:小于xx的第一个数(接近xx)
int bin_s(int xx) { int l=1,r=10; int ans=-1; while(l<=r){ int m=(l+r)>>1; if(a[m]>=xx) r=m-1; else ans=m,l=m+1; } return ans; } e.g: int a[]={0,1,2,5,5,5,5,7,8,9,10}; <pre name="code" class="cpp">in: 5 out: 2
标签:style blog color sp strong on 2014 log as
原文地址:http://blog.csdn.net/code_or_code/article/details/41686031