主题:一个小问题,请大家帮我看一下
二分查找:我按从小到大的顺序输入9个数,为什么当我查找的数值比最后一个值还要大时结果返回是第9个数,应该查找不到才对呀
#include<stdio.h>
void find(int array[],int n,int e)
{int low=1,high=n,mid;
while(low<=high)
{mid=(low+high)/2;
if(e==array[mid]) {printf("x is the %d number!\n",mid);break;}
else if(e<array[mid]) high=mid-1;
else low=mid+1;
}
if(low>high) printf("x is not exist!\n");
}
void main()
{int a[9],i,x;
printf("please input the 9 numbers:\n");
for(i=1;i<10;i++)
scanf("%d",&a[i]);
printf("input x:");
scanf("%d",&x);
find(a,9,x);
}
#include<stdio.h>
void find(int array[],int n,int e)
{int low=1,high=n,mid;
while(low<=high)
{mid=(low+high)/2;
if(e==array[mid]) {printf("x is the %d number!\n",mid);break;}
else if(e<array[mid]) high=mid-1;
else low=mid+1;
}
if(low>high) printf("x is not exist!\n");
}
void main()
{int a[9],i,x;
printf("please input the 9 numbers:\n");
for(i=1;i<10;i++)
scanf("%d",&a[i]);
printf("input x:");
scanf("%d",&x);
find(a,9,x);
}