折半递归,输出怎么都是错的,但是又看不出错在哪里,求助!
本帖最后由 努力学习只是 于 2017-10-14 15:40 编辑折半递归,输出怎么都是错的,但是又看不出错在哪里,求助!
#include"iostream"
using namespace std;
int a={1,2,3,14,15,23,26,29,35,41,49};
int bresearch(int b,int low,int high){
if(low<=high){
int mid=(low+high)/2;
if(a==b)return mid+1;
if(a>b)
bresearch(b,low,mid-1);
if(a<b)bresearch(b,mid+1,high);
}
}
int main(){
int n,key,high,low;
n=11;
high=n-1;
low=0;
cout<<"input a number:";
cin>>key;
cout<<bresearch(key,low,high);
return 1;
}
页:
[1]