#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int k = 0;
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0;//设定左值
int right = sz - 1;//设定右值
printf("请输入您要查找的数字:");
scanf("%d", &k);
while (left <= right)//循环条件
{
int mid = left+ (right - left) / 2;//通过左右值求取中间值
if (k > arr[mid])//如果k大于mid下标对应的值,那么左值变为mid+1
{
left = mid + 1;
}
else if (k< arr[mid])//如果k小于mid下标对应的值,那么右值变为mid-1
{
right = mid - 1;
}
if (k == arr[mid])
{
printf("找到了 下标为%d\n",mid);
break;
}
}
if(left>right)
printf("数组中不存在该数!\n");
return 0;
}