看了整整一上午就是不知道到底是哪里出了错。
#include<stdio.h>
int binreseach(int v[],int n,int number)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(high+low)/2;
if(number>v[mid])
low=mid;
else if(number<v[mid])
high=mid;
else
return 1;
}
return 0;
}
main()
{
int v[20],number,m,n,i;
printf("input:the array have n numbers\n");
scanf("%d",&n);
printf("input the array number:\n");
for(i=0;i<n;i++)
scanf("%d",&v[i]);
printf("input the find number:\n");
scanf("%d",&number);
m=binreseach(v,n,number);
if(m>0)
printf("find the number:\n");
else
printf("can't find the number:\n");
}
这是个折半查找的程序,当输入的数据是在v数组中的时候,程序正常运行,但是当输入的number不是在v数组的时候,按回车键,程序始终没有反应。
折半法查询程序
[解决办法]
int binreseach(int v[],int n,int number)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(high+low)/2;
if(number>v[mid])
low=mid + 1;
else if(number<v[mid])
high=mid - 1;
else
return 1;
}
return 0;
}