1.实践题目：输入n值(1<=n<=1000)、n个非降序排列的整数以及要查找的数x，使用二分查找算法查找x，输出x所在的下标（0~n-1）及比较次数。若x不存在，输出-1和比较次数。

#include<iostream>
using namespace std;
int Search (int a[],int n, int x,int &t)
{

int l = 0;
int r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (x == a[mid]) {
t++;
return mid;
}
if (x > a[mid]) {
t++;
l = mid + 1;
} else {
t++;
r = mid - 1;}
}
return -1;
}
int main()
{
int n;
int a[1000];
int x,t = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> x;

cout << Search (a, n, x, t)<<endl;

cout << t;

return 0;
}

#include<iostream>
using namespace std;

int BIN(int a[], int key, int n) {
int left = 0;
int right = n - 1;

int i = 0;
int j = 0;
while (left <= right) {
int middle = (left + right) / 2;

if (key == a[middle])
{
i = j = middle;
cout << i <<" "<<j<<endl;

//cout << t;
return middle;
}
if (key > a[middle])left = middle + 1;
else { right = middle - 1; }

}
i = right;
j = left;
cout << i<<" "<< j<<endl;

return -1;
}

int main() {
int n;
int x;
cin >> n>> x;

int *a = new int [n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}

BIN(a, x, n);

system("pause");
}

第三题课上并没有来得及看，课后和partner一起打的时候大概理清了思路

1 边界点的等号是否取到，中间点的位置是否可取

2 二分的对象该如何妥当处理