#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int num=sizeof(a)/sizeof(a[0]);
int left =0;
int right=num-1;
int mid=0;
int n=0;
scanf("%d",&n);
while(left<=right)
{
mid=(left+right)/2;//这个条件只能在这,每次循环更新一次
if(a[mid]<n)
{
left=mid+1;
}
else if(a[mid]>n)
{
right=mid-1;
}
else break;
}
if(left<=right)
{
printf("find it! %d",mid);
}
else
printf("not find it!");
}
折半查找法
最新推荐文章于 2024-08-12 00:08:19 发布