有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;
}
}
...全文
2911 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...
训练据保存为deep_convnet_params.pkl,UI使用wxPython编写。卷积神经网络(CNN)是一种专门针对图像、视频等结构化据设计的深度学习模型,在计算机视觉、语音识别、自然语言处理等多个领域有广泛应用。其核心设计理念源于对生物视觉系统的模拟,主要特点包括局部感知、权重共享、多层级抽象以及空间不变性。 **1. 局部感知与卷积操作** 卷积层是CNN的基本构建块,使用一组可学习的滤波器对输入图像进行扫描。每个滤波器在图像上滑动,以局部区域内的像素与滤波器权重进行逐元素乘后求和,生成输出。这一过程能够捕获图像中的边缘、纹理等局部特征。 **2. 权重共享** 同一滤波器在整个输入图像上保持相同的权重。这显著减少了模型参量,增强了泛化能力,并体现了对图像平移不变性的内在假设。 **3. 池化操作** 池化层通常紧随卷积层之后,用于降低据维度并引入空间不变性。常见方有最大池化和平均池化,它们可以减少模型对微小位置变化的敏感度,同时保留重要特征。 **4. 多层级抽象** CNN通常包含多个卷积和池化层堆叠在一起。随着网络深度增加,每一层逐渐提取更复杂、更抽象的特征,从底层识别边缘、角点,到高层识别整个对象或场景,使得CNN能够从原始像素据中自动学习到丰富的表示。 **5. 激活函与正则化** CNN中使用非线性激活函来引入非线性表达能力。为防止过拟合,常采用正则化技术,如L2正则化和Dropout,以增强模型的泛化性能。 **6. 应用场景** CNN在诸多领域展现出强大应用价,包括图像分类、目标检测、语义分割、人脸识别、图像生成、医学影像分析以及自然语言处理等任务。 **7. 发展与演变** CNN的概念起源于20世纪80年代,其影响力在硬件加速和大规模据集出现后真正显现。经典模型如LeNet-5用于手写字识别,而AlexNet、VGG、GoogLeNet、ResNet等现代架构推动了CNN技术的快速发展。如今,CNN已成为深度学习图像处理领域的基石,并持续创新。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!

70,037

社区成员

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

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