#include
#include
using namespace std;
int half(int,vector );
int low=0;
int high=7;
int main(){
int b[10]={1,10,15,17,18,21,22,35,54,65};
vector a(b,b+10);
cout<<"請輸入要査找的數:";
int number;
cin>>number;
if(half(number,a)){
cout<<"ok";
}
return 0;
}
int half(int number,vector <int> a){
int mid=(high-low)/2;
if(number>a[mid]){
a[low]=a[mid+1];
half(number,a);
}
else if(number<a[mid]){
a[high]=a[mid-1];
half(number,a);
}
else if (number==a[mid]){
return 1;
}
}
a[high]=a[mid-1],這樣做完全沒有意義吧。應該是high=mid-1,對於low也是low=mid+1