有15个数按由小到大的顺序存放在一个数组中,输入一个数,用折半查找法找出它是数组中的第几个元素的值。如果不在的话,就打印“没有找到”

nothingiknow 2007-12-08 02:52:33
在我的教材里看到的这个程序,怎么运行时,如果数组里有的数就正确呢? 当数组里没有这个数时运行就不正确了?就不能按照要求显示!这是怎么回事啊?请教下!谢谢各位
#define N 15
void main()
{ int i,num,top,bott,mid,loca,a[N],flag=1,sign=1;
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:");
}
printf("\n");
for(i=0;i<N;i++)
printf("%4d",a[i]);
printf("\n");
flag=1;
while(flag)
{printf("Input number to look for:");
scanf("%d",&num);
loca=0;
top=0;
bott=N-1;
if((num<a[0])||(num>a[N-1]))
loca=-1;
while((sign==1)&&(top<=bott))
{mid=(top+bott)/2;
if(num==a[mid])
{loca=mid;
printf("Find %d, its position is %d\n",num,loca+1);
sign=0;
}
else if(num<a[mid])
bott=mid-1;
else
top=mid+1;
}
if(sign==1||loca==-1)
printf("%d is not found.\n",num);
printf("Continue or not (Y/N)?");
scanf("%c",&c);
if(c=='N'||c=='n')
flag=0;
}
}
...全文
2927 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
san_dan 2007-12-08
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <iostream.h> //下面用到了cin
#define N 15
void main()
{ int i,num,top,bott,mid,loca,a[N],flag=1,sign=1;
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:");
}
printf("\n");
for(i=0;i <N;i++)
printf("%4d",a[i]);
printf("\n");
flag=1;
while(flag)
{printf("Input number to look for:");
scanf("%d",&num);
loca=0;
top=0;
bott=N-1;
if((num <a[0]) ¦ ¦(num> a[N-1]))
loca=-1;
while((sign==1)&&(top <=bott))
{mid=(top+bott)/2;
if(num==a[mid])
{loca=mid;
printf("Find %d, its position is %d\n",num,loca+1);
sign=0;
}
else if(num <a[mid])
bott=mid-1;
else
top=mid+1;
}
if(sign==1 ¦ ¦loca==-1)
printf("%d is not found.\n",num);
sign=1; //我觉得这里要加上这条,要不然下面选Y时不能执行到正确结果
printf("Continue or not (Y/N)?");
cin>>c; //这里用scanf("%c",&c);起不到选择的效果.
if(c=='N' ¦ ¦c=='n')
flag=0;
}
}
nothingiknow 2007-12-08
  • 打赏
  • 举报
回复
谢谢哈
srq1625 2007-12-08
  • 打赏
  • 举报
回复
#include<stdio.h>//你注意到你的程序里其实没有文件头。

#define N 15
void main()
{ int i,num,top,bott,mid,loca,a[N],flag=1,sign=1;
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:");
}
printf("\n");
for(i=0;i <N;i++)
printf("%4d",a[i]);
printf("\n");
flag=1;
while(flag)
{printf("Input number to look for:");
scanf("%d",&num);
loca=0;
top=0;
bott=N-1;
if((num <a[0])||(num> a[N-1])) //这里的“或”运算符你要注意啊。你写的错了。
loca=-1;
while((sign==1)&&(top <=bott))
{mid=(top+bott)/2;
if(num==a[mid])
{loca=mid;
printf("Find %d, its position is %d\n",num,loca+1);
sign=0;
}
else if(num <a[mid])
bott=mid-1;
else
top=mid+1;
}
if(sign==1||loca==-1)
printf("%d is not found.\n",num);
printf("Continue or not (Y/N)?");
scanf("%c",&c);
if(c=='N' ||c=='n')
flag=0;
}
}
我运行了一下,好了。继续加油啊,o(∩_∩)o...
代码下载地址: https://pan.quark.cn/s/692a61fb005f HTML(超文本标记语言)是一种构建网页的标准化标记语言,它构成了网页设计的核心,使得开发者能够通过文本、图像、视频等媒介创建交互式的用户界面。在名为"HTML淘宝网页"的项目中,我们可以研究HTML在模拟知名电子商务平台——淘宝网上的实际应用,以及相关的前端开发技术。HTML页面的构造通常包含头部(head)和主体(body)两个主要部分。在头部部分,开发者会加载CSS(层叠样式表)和JavaScript文件,这些文件用于调控页面的外观和交互功能。在"淘宝网页"的开发过程中,开发者或许运用了内联样式、内部样式表或外部样式表来设定页面的视觉表现,使其与淘宝网站的风格相吻合。主体部分是展示网页内容的核心区域,其中包含了多种HTML组件,例如`
`(页头)、`

70,040

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧