#include<iostream>
using namespace std;
#define Length 10
int main() {
//数组
int nums[Length] = { 0,1,2,3,4,5,6,7,8,9};//二分法需要数组有序
int Min = 0;
int Max = Length - 1;
int Mid=0;
int result = 0;
int target = 0;
cout << "请输入要查询的数字:" << endl;
cin >> target;
//二分查询
while (true) {
//如果Min>Max,说明该数组没有要查询的目标
if (Min > Max) {
result= -1;
break;
}
Mid = (Min + Max) / 2;
if (target > nums[Mid]) {
Min=Mid+1;
}
else if (target < nums[Mid]) {
Max=Mid-1;
}
else {
result = Mid;
break;
}
}
cout << "要查询的数字在数组中的索引为:" << result;
return 0;
}