| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 342 人关注过本帖
标题:小菜的一个关于二分法得问题。。。急
收藏  订阅  推荐  打印 
早起的月光
Rank: 1
等级:新手上路
帖子:9
积分:196
注册:2007-11-6
小菜的一个关于二分法得问题。。。急

#include<stdio.h>
int search(int array[],int i);
int main()
{
    int i,j,result;
    int array[10]={1,2,3,4,5,6,7,8,9,10};
    
    printf("please enter the number you want to search");
    while(1){
        scanf("%d",&i);
        if(i<1||i>10){
            printf("please enter the number from 1 to 10");
        }else{
            break;
        }
    result=search(array,i);
    printf("the result is %d",result);
    return 0;
}
int search(int array[],int i)
{
    int mid,low,upper;
    low=0;
    upper=10;
    
    mid=(low+upper)/2;
    while(low<upper){
        if(array[mid]==i)
            return mid;
        else if(array[mid]>i)
            upper=mid-1;
        else
            low=mid+1;
        mid=(low+upper)/2;
    }
}
调试时总是显示 mid low upper 未声明,是什么原因啊??
搜索更多相关主题的帖子: 二分法  小菜  
2008-1-19 22:02
xianshizhe111
Rank: 6Rank: 6
等级:金牌会员
帖子:1451
积分:15818
注册:2007-12-8
/*你看这样还报错不*/

#include<stdio.h>
int search(int array[],int i);
int main()
{
    int i,j,result;
    int array[10]={1,2,3,4,5,6,7,8,9,10};
   
    printf("please enter the number you want to search");
    while(1){
        scanf("%d",&i);
        if(i<1||i>10){
            printf("please enter the number from 1 to 10");
        }else{
            break;
        }
     }  /*循环少了 }  */
    result=search(array,i);
    printf("the result is %d",result);
    return 0;
}
int search(int array[],int i)
{
    int mid,low,upper;
    low=0;
    upper=10;
   
    mid=(low+upper)/2;
    while(low<upper){
        if(array[mid]==i)
            return mid;
        else if(array[mid]>i)
            upper=mid-1;
        else
            low=mid+1;
        mid=(low+upper)/2;
    }
}
2008-1-19 22:14
早起的月光
Rank: 1
等级:新手上路
帖子:9
积分:196
注册:2007-11-6

哦。。。。。我说呢。不过为什么会报那种错误呢???
2008-1-19 22:22
xianshizhe111
Rank: 6Rank: 6
等级:金牌会员
帖子:1451
积分:15818
注册:2007-12-8

循环体不全while(1)
        {
        scanf("%d",&i);
        if(i<1||i>10)
        {
            printf("please enter the number from 1 to 10");
        }
        else
        {
            break;
        }
    }   
/*你看少了最后的 } 循环体能成立吗? */
2008-1-19 22:31
早起的月光
Rank: 1
等级:新手上路
帖子:9
积分:196
注册:2007-11-6

不成立,得教了。谢了哈。
2008-1-19 22:35
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.167767 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved