二分法查找数据所在位置
流程图:
伪代码:
源代码:
#include<stdio.h>
#define N 10
int 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");
for(i=0;i<N;i++)
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 be found!\n");
return 0;
}
标签:练习,mid,c++,else,high,low,printf,打卡 From: https://www.cnblogs.com/HD-Kudu-2219/p/17327461.html