| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 580 人关注过本帖
标题:2分查找,递归实现
取消只看楼主 加入收藏
bw4243
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
2分查找,递归实现
谁帮我看下代码哪里错了
#include <stdio.h>
#include <stdlib.h>
#define N 11
#define COMPARE(x,y) (x)<(y)?-1:((x)==(y)?0:1)
int binsearch(int searchnum,int list[],int left,int right);
int main()
{
     int list[N]={1,3,5,8,10,14,17,19,23,26,29};
     int ele,conse;
     printf("input a number searching for.");
     scanf("%d",&ele);
     conse=binsearch(ele,list,0,11);
     if(conse==-1)
                   fprintf(stderr,"error,have no equal value.");
     else
                   printf("%d is found in list.",ele) ;     
   
}
int binsearch(int searchnum,int list[],int left,int right)
{
    int middle;
    if(left<=right){
                       middle=(left+right)/2;
                       switch(COMPARE(list[middle],searchnum)){
                       case-1:    return
                              binsearch(searchnum,list,middle+1,right);
                       case 0:   
                       return   middle;
                       case 1:    return
                               binsearch(searchnum,list,left,middle-1);
                                                               }      
                     }                     
              return -1;

}                                                                                    
                                                                                                  }
搜索更多相关主题的帖子: searching include number 
2011-06-30 15:07
bw4243
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-28
收藏
得分:0 
回复 2楼 Pirelo
我加了return 0;后C-FREE还是报错,DEV则运行无结果
2011-06-30 17:06
快速回复:2分查找,递归实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031048 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved