#include<stdio.h>标签:二分,arr,right,int,mid,查找,printf,left From: https://blog.51cto.com/u_13606048/6088627
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int left = 0;
int max = sizeof(arr) / sizeof(arr[0]);
int right = max - 1;
int x = 0;
printf("输入你想要查询的数值:");
scanf_s("%d", &x);
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == x) {
printf("%d在数组中第%d个位置上", x, mid + 1);
break;
}
else if (arr[mid] < x) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
if (left > right) {
printf("未能找到该数值");
}
return 0;
}