#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int arr[10] = { 1,56,33,69,84,2,30,99,512,321 };
int low = 0, high = 9;
sort(arr, arr + 10);
int i, j, k;
cout << "此时数据为:" << endl;
for (i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
int n;
cin >> n;
for (i = (low + high) / 2; low < high; i = (low + high) / 2)
{
if(arr[i] > n)
high = i - 1;
else if(arr[i] < n)
low = i + 1;
else
break;
}
if(n != arr[i])
cout << "未找到!" << endl;
else
cout << "当前下标为:" << i;
return 0;
}
标签:折半,arr,cout,int,else,high,查找,low From: https://www.cnblogs.com/atrue/p/17354005.html