请教 开关变量的问题———折半查找法
15个数按从大到小的顺序存放在一个数组中。输入一个数,要求用折半查找法找出该数在程序最后 为什么程序跳过输入N/Y 而直接执行 input number to look for?
下面是程序
#include<stdio.h>
#define N 15
void main()
{int i,number,top,bott,mid,loca,a[N],flag=1,sign;
char c;
printf("enter the 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:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%d ",a[i]);
printf("\n");
while(flag)//flag为开关变量,控制程序是否结束运行
{
printf("input number to look for:");
scanf("%d",&number);
sign=0;
top=0;
bott=N-1;
if((number<a[0])||number>a[i-1])
loca=-1;
while((!sign)&&(top<=bott))
{
mid=(top+bott)/2;
if(number==a[mid])
{
loca=mid;
printf("has found %d,its position is %d\n",number,loca+1);
sign=1;
}
else if(number<a[mid])
bott=mid-1;
else
top=mid+1;
}
if(!sign||loca==-1)
printf("can't find %d.\n",number);
printf("continue or not(Y/N)?");
scanf("%c",&c);//这儿把输入格式的%c 误写成%d有影响吗
if(c=='N'||c=='n')
flag=0;
}
}