| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 580 人关注过本帖
标题:2分查找,递归实现
只看楼主 加入收藏
bw4243
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
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
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:3 
在main()函数末位加上“return 0;”吧
2011-06-30 15:45
bw4243
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-28
收藏
得分:0 
回复 2楼 Pirelo
我加了return 0;后C-FREE还是报错,DEV则运行无结果
2011-06-30 17:06
fragileeye
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:107
专家分:387
注 册:2011-5-21
收藏
得分:3 
版本呢?5.0无错。
2011-06-30 17:09
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:3 
程序代码:
#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(void)
{
    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);
    return 0;

}

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;
}

// 你最后多了个 }

—>〉Sun〈<—
2011-07-01 18:22
shinan77
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:87
专家分:188
注 册:2010-9-24
收藏
得分:3 
fprintf(stderr,"error,have no equal value.");
里面的stderr是什么

--------将学到的东西为我所用,这才是学习的目的 --------
2011-07-01 22:56
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:3 
回复 6楼 shinan77
标准错误流

My life is brilliant
2011-07-02 10:52
sc19861202
Rank: 2
来 自:唐山
等 级:论坛游民
帖 子:23
专家分:47
注 册:2011-3-19
收藏
得分:3 
学习一下!
2011-07-02 18:11
水古奇影
Rank: 2
等 级:论坛游民
帖 子:16
专家分:78
注 册:2011-5-16
收藏
得分:3 
#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)
                   printf("error,have no equal value.");//改正printf格式输出
     else
                   printf("%d is found in list.",ele) ;  
     return 0; //返回一个0  
   
}
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;

}
2011-07-02 22:31
快速回复:2分查找,递归实现
数据加载中...
 
   



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

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