问题:有15个数按由小到大的顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数组中第几个元素的值.
源程序如下:
#include
#define N 15
main()
{int i,number,top,mid,bott,loca,j,a[N],flag=1,sign=1;
char c;
printf("enter date:\n");
scanf("%d",&a[0]);
i=1;
while(i=a[i-1])
i++;
else
printf("enter this data again:");
}
printf("\n");
for(i=0;ia[N-1]))
loca=-1;
while((sign==1)&&(top<=bott))
{mid=(bott+top)/2;
if(number==a[mid])
{loca=mid;
printf("find %d,its position is %d \n",number,loca+1);
sign=0;
}
else if(number<a[mid])
bott=mid-1;
else
top=mid+1;
}
if(loca==-1||sign==1)
printf("not found.\n");
printf("continue or not(Y/N)?");
scanf(" %c",&c);
if(c=='N'||c=='n')
flag=0;
}
getch();
}
疑问:倒数第六行的"scanf(" %c",&c)"语句中%c前的空格的作用是什么?为什么不加空格就不能实现.
请高手详细指点,谢过!
|