3.代码示例
#include<iostream> using namespace std; int main(){ int a[10]={2,3,4,5,6,7,8,9,12,18}; int i; for(i=0;i<10;i++){ cout<<a[i]<<" "; } cout<<endl; int x,l,r,mid; cin>>x; l=0;r=10; while(l<=r){ mid=(l+r)/2; if(x<a[mid]){ r=mid-1; } else if(x>a[mid]){ l=mid+1; } else{ cout<<"下标值为:"<<mid+1; break; } } if(a[mid]!=x) cout<<"Not be found"; return 0; }
4.结果输出
标签:std,10,示例,int,Day9,mid From: https://www.cnblogs.com/cream867/p/17337717.html