#include <stdio.h>
int mid(int a[], int x, int len)
{
int left,right,mid;
left=0;
right=len-1;
while(left<=right)
{
mid=(left+right)/2;
if(a[mid]==x)
{
return mid;
}
if(a[mid]<x)
{
left=mid+1;
}
else
{
right=mid-1;
}
}
return -1;
}
int main()
{
int a[]={1,2,3,4,5,6,7,8};
int x,num;
scanf("%d",&x);
num=mid(a,x,8);
printf("%d\n",num);
return 0;
}
二分法查找数组中一个数
最新推荐文章于 2022-07-21 22:31:02 发布