//二分查找法2022.1.21
#include<stdio.h>
int main(){
int a;
int arr[9] = {1,2,3,4,5,6,7,8,9};
int flag=1;
while(flag){
int sz = sizeof(arr)/sizeof(arr[0]);
int left = 0;
int right = sz-1;
int mid = (left+right)/2;
printf("请输入你要查找的数>");
scanf("%d",&a);
while(left<=right){
if(a==arr[mid]){
printf("你要查找的数的下标为%d\n",mid);
flag = 0;
break;
}
if(a>arr[mid]){
left = mid+1;
mid = (left+right)/2;
}
if(a<arr[mid]){
right = mid-1;
mid = (left+right)/2;
}
}
if(left>right){
printf("找不到,请重新输入\n");
}
}
return 0;
}
二分查找法的c-code
于 2022-01-21 20:33:26 首次发布