| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 430 人关注过本帖
标题:求助啊,有关于折半查找的问题
取消只看楼主 加入收藏
autumnyellow
Rank: 2
等 级:论坛游民
帖 子:72
专家分:75
注 册:2015-4-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
求助啊,有关于折半查找的问题
# include <stdio.h>
# define N 15
int main ()
{
    int i,number,top,bott,mid,loca,a[N],flag =1,sign;
    char c;
    printf("enter data :\n");
    scanf("%d",&a[0]);
    i =1;
    while(i<N)
    {
        scanf("%d",&a[i]);
        if (a[i]>=a[i-1])
            i++;
        else
            printf("enter this data again:\n");

    }
    printf("\n");
    for (i =0;i<N;i++)
        printf("%5d",a[i]);
    printf("\n");
    while (flag)
    {
        printf("input number to look for:");
        scanf("%d",&number);
        sign =0;
        top = 0;
        bott = N-1;
        if ((number<a[0] )|| (number>a[N-1]))
           loca=-1;
        while (!sign && top<=bott)
        {
            mid =(bott+top)/2;
            if (number==a[mid])
            {
                loca= mid;
                printf("has found %d,its position is %d\n",number,loca+1);
                sign =1;
            }
            else if(number<a[mid])
                bott =mid-1;
            else
                top = mid+1;

        }
        if( !sign || loca==-1)
            printf("cannot find %d\n",number);
        printf("continue or not(Y/N)");
        scanf("%c",&c);
        if(c=='N'||c=='n')
            flag = 0;

    }
    return 0;
}
/*输出为:
enter data :
1
3
4
5
6
8
12
23
34
44
45
56
57
58
68

    1    3    4    5    6    8   12   23   34   44   45   56   57   58   68
input number to look for:7
cannot find 7
continue or not(Y/N)input number to look for:n
cannot find 7
continue or not(Y/N)请按任意键继续. . .



其中while (!sign && top<=bott)中!sign为什么为0,这程序中的sign值变化不理解,求指点·····
搜索更多相关主题的帖子: include number 
2015-04-26 21:10
autumnyellow
Rank: 2
等 级:论坛游民
帖 子:72
专家分:75
注 册:2015-4-14
收藏
得分:0 
怎么还没人回啊!!!!!!!!!!
2015-04-26 22:34
快速回复:求助啊,有关于折半查找的问题
数据加载中...
 
   



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

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