高分哦:帮忙下!这个程序有啥错误呀???为什么每次都输出找不到啊?

来源:百度知道 编辑:UC知道 时间:2024/06/02 06:24:08
main()
{
int a[10],mid,bot,top,x,i,find;
printf("please input the array:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("please input the numbers you want find:\n");
scanf("%d",&x);
printf("\n");
bot=0;top=9;find=0;
while((bot<=top)&&(find==0))
{
mid=(top+bot)/2;
if(x==a[mid])
{find=1;break;}
else if(x<a[mid])
top=mid-1;
else bot =mid+1;
}
if(find==1)
printf("the numbers %d if found : the a[%d]",x,mid);
else
printf("the numbers is not found!\n");
}

采用折半查找,首先要排序

printf("please input the numbers you want find:\n");

main()
{
int a[10],mid,bot,top,x,i,find;
printf("please input the array:\n");

for(i=0;i<10;i++)
scanf("%d",&a[i]);

printf("please input the numbers you want find:\n");
scanf("%d",&x);
printf("\n");

bot=0;top=9;find=0;

while((bot<=top)&&(find==0))
{
mid=(top+bot)/2;
if(x==a[mid])
{
find=1;
}
else if(x<a[mid])
top=mid-1;
else bot =mid+1;
}

if(find==1)
printf("the numbers %d if found : the a[%d]",x,mid);
else
printf("the numbers is not found!\n");
}

以后写程序注意点~~别把智能abc的括号放到程序里来~
你在仔细看看你程序里的 while((bot<=top)&&(find==0)) 这句话的括号吧~

不知道~~ 想想~~
你这程序什么意思啊~~