[求助]折中查询程序.怎么运行没结果的啊
#include <stdio.h>int main()
{
int item[5]={10,11,12,13,14};
int low=0,high=4,x,mid;
scanf("%d",&x);
while(low<=high)
{
mid=(low+high)/2;
if(item[mid]==x)
break;
else if(x>item[mid])
low=mid+1;
else
high=mid-1;
}
return mid;
}
请高手知道下!!!
----------------解决方案--------------------------------------------------------
#include <stdio.h>
int main()
{
int item[5]={10,11,12,13,14};
int low=0,high=4,x,mid;
scanf("%d",&x);
while(low<=high)
{
mid=(low+high)/2;
if(item[mid]==x)
break;
else if(x>item[mid])
low=mid+1;
else
high=mid-1;
}
printf("第%d个元素",mid+1);/*你没有输出函数*/
}
----------------解决方案--------------------------------------------------------
能帮我解释下:
low=mid+1;
high=mid-1;
的运算吗,我不能理解
x>item[mid]
low=mid+1;
在这里mid=(low+high)/2=(10+14)/2=12
当x=14=item[4]的时候,x>item[mid]
那么:low=mid+1在这里是什么作用啊
[此贴子已经被作者于2007-2-14 13:21:16编辑过]
----------------解决方案--------------------------------------------------------
使的它的座标向后退.直到low>high,那么mid确定了
----------------解决方案--------------------------------------------------------