sinat_33936122
Jonclott
采纳率100%
2016-04-21 11:38 阅读 1.2k

折半查找程序,输入后就崩溃,谁能帮忙看一下

10

#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;
    }       
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    whoSaidNo whoSaidNo 2016-04-21 12:08

    a[high]=a[mid-1],这样做完全没有意义吧。应该是high=mid-1,对于low也是low=mid+1

    点赞 评论 复制链接分享
  • niu112233688 niu112233688 2016-04-21 11:51

    half函数可能没有返回值,前两个if,else应加上return语句

    点赞 评论 复制链接分享

相关推荐