C程序输出和题意不相符,问修改?

jasmine jasmine 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目:有15个数按由小到大的顺序存放在一个数组中。输入一个数,要求用折半查找法找出该数是数组中第几个元素的值,
如果该数不在数组中,输出“不在表中”。
要求:在我的程序上进行修改

#define N 15
#include<stdio.h>
void main()
{int number ,top,bott,mid,a[N],sign,flag=1,i=0,loca;
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 the data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");

while(flag)
{ printf("the number to look for is:\n");
scanf("%d",&number);
top=0;bott=N-1;sign=0;
if(number<a[0]||number>a[N-1]) loca=-1;
while((!sign)&&(top<=bott))
{mid=(top+bott)/2;
if(number==a[mid]) {
loca=mid;
printf("Has found %d,the 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("can not find %d.\n ",number);
printf("continue or not(Y/N)?\n");
scanf("%c",&c);
if(c=='N'||c=='n')
flag=0;
}

system("pause");
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
jasmine
C语言 70,039 成员 243,237 内容