标题:折半找数字
取消只看楼主
Zhijian01
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2012-12-29
结帖率:66.67%
已结贴  问题点数:20 回复次数:1 
折半找数字
这是书上的程序,我怎么找不到,运行了,出现的问题是找不到
#include "stdio.h"
#define n 15
void main()
{
    int i,number,top,bott,mid,loca, a[n],flag=1,sign;
    char c;
    printf("shu ru shu zhi:\n");
     scanf("%d",&a[0]);
     i=1;
      while (i<n)
      {
         scanf("%d",&a[i]);
         if (a[i]>=a[i-1])
              i++;
          else printf("chong xin shu ru:\n");
      }
    printf("\n");
     for(i=0;i<n;i++)
         printf("%3d",a[i]);
     printf("\n");
     while (flag)
     {printf("yao zhao de shu zhi:" );
     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("zhao dao le:%d shi zai di %d ge shu zhi.",number,loca+1);
         sign=1;
     }
     else if(number<a[mid])
         bott=mid-1;
     else top=mid+1;
     
     }
     if((!sign)||(loca==-1))
         printf("mei you zhao dao  %d \n",number);
     printf("shi fou ji xu zhao?y/n?");
      scanf(" %c",&c);
     if((c=='n')||(c=='N'))
         flag=0;
     
     
     }

}
运行结果就是找不到,请高手帮忙,谢谢了
搜索更多相关主题的帖子: include number 
2013-01-02 08:50
Zhijian01
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2012-12-29
得分:0 
谢谢您了,我试试看
2013-01-02 12:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-394798-1-1.html




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

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