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