基础C语言简单填空题

有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=(bott+top)/2; if (number= =a[mid]) {loca=mid; printf("Has found %d, its position is %d\n", number, (6) ); sign=1; } else if (number<a[mid]) bott= (7) ; else top=mid+1; } if ( (8) ) printf("can not find %d.\n",number); printf("continue or not(Y/N)?"); scanf("%c",&c); if (c=='N'||c=='n') (9) ; } }

第1个回答  2019-05-28
好像应该是要求从小到大排列的才行。。。囧
i<N
a[i]<a[i-1]
1
N-1
top<=bott
loca
mid-1
loca==-1
break