C语言 填空题!(在线等)

来源:百度知道 编辑:UC知道 时间:2024/09/21 22:45:14
有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。
#include <stdio.h>
#define N 15
main()
{ int i, number, top, bott, mid, loca,
a[N],flag=1,sign;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while ( (1) )
{scanf("%d",&a[i]);
if ( (2) )
i++;
else
printf("enter this data again:\n");
}
printf("\n");
for (i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
while( (3) )
{printf("input number to look for:");
scanf("%d",&number);
sign=0;
top=0;
bott= (4) ;
if ((number<a[0])||(number>a[N-1]))
loca=-1;
while ((!sign)&& ( (5) )
{mid=(

好像是一样的题,那么我一样的回答吧。。。
好像应该是要求从小到大排列的才行。。。囧
i<N
a[i]<a[i-1]
1
N-1
top<=bott
loca
mid-1
loca==-1
break

给,已经编译运行确认:
#include <stdio.h>
#define N 15
main()
{ int i, number, top, bott, mid, loca,
a[N],flag=1,sign;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while (i<N) // (1)
{scanf("%d",&a[i]);
if (a[i]>=a[i-1]) // (2)
i++;
else
printf("enter this data again:\n");
}
printf("\n");
for (i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
while(flag) // (3)
{printf("input number to look for:");
scanf("%d",&number);
sign=0;
top=0;
bott=N-1; // (4)
if ((number<a[0])||(number>a[N-1]))
loca=-1;
while ((!sign)&& (top<=bott))//