标题:折半找数字
只看楼主
Zhijian01
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2012-12-29
结帖率:66.67%
已结贴  问题点数:20 回复次数:4 
折半找数字
这是书上的程序,我怎么找不到,运行了,出现的问题是找不到
#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
古丁高手
Rank: 1
来 自:地狱
等 级:新手上路
帖 子:56
专家分:8
注 册:2012-8-8
得分:0 
建议用函数来搞这样看的我想找猫哥!

不要管我是谁,我也不管你是谁!
just kiss myass!
2013-01-02 09:10
a151141
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:197
专家分:680
注 册:2012-10-19
得分:20 
错误已经标明,如下:
#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;       /*这错了,该是top=n-1 */
      bott=n-1;     /*这也错了,该是bott=0 */
      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;
      
      
      }
 
}

世界上幸福的事就是抓到一只羊,更幸福的事就是抓到两只羊……
2013-01-02 09:27
a151141
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:197
专家分:680
注 册:2012-10-19
得分:0 
改正后试试吧

世界上幸福的事就是抓到一只羊,更幸福的事就是抓到两只羊……
2013-01-02 09:28
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.015663 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved