#include<iostream>
using namespace std;
int find1(int a[],int val)
{
int l=0,r=5;
while(l<r)
{
int mid=l+r>>1;
if(a[mid]>=val) r=mid;//砍右
else l=mid+1;
}
return r+1;
}
int find2(int a[],int val)
{
int l=0,r=5;
while(l<r)
{
int mid=l+r+1>>1;
if(a[mid]<=val) l=mid;//砍左
else r=mid-1;
}
return r+1;
}
int main()
{
int a[6]={20,22,24,26,28,30};
int n=10;
srand(time(0));
while(n--)
{
int val=20+rand()%10;
int idx1=find1(a,val);
int idx2=find2(a,val);
cout<<val<<"(index):"<<idx2<<" "<<idx1<<endl;
}
return 0;
}
整数二分
最新推荐文章于 2024-09-17 23:40:04 发布