问题描述:N个有序数数列已放在一维数组中,利用二分查找法找整数m在数组中的位置,若找到,则输出其下标值;反之,则输出“Not be found!".
完整程序:
#include<stdio.h>
#define N 10
main()
{
int i,a[N]={-3,4,7,9,13,45,67,89,100,180},low=0,high=N-1,mid,k=-1,m;
printf{"a数组中的数据如下:\n"};
printf(("%d",a[i]};
printf("\n"};
printf("Enter m:");
scanf("%d",&m);
while(low<=high)
{
mid=(low+high)/2;
if(m<a[mid])
high=mid-1;
else
if(m>a[mid])
low=mid+1;
else
{
k=mid;
break;
}
}if(k>=0)
printf("m=%d,index=%d\n",m,k);
else
printf("Not ba found!\n"}
}
标签:折半,mid,else,high,查找,low,printf From: https://www.cnblogs.com/zjm921/p/17353924.html