循环问题
折半法#include<stdio.h>
#define N 5
int main()
{
int a[N],x,y,i,begin,end,flag; char c;
printf("input %d numbers:\n",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
flag=1;
while(flag)
{
printf("please enter the number:\n");
scanf("%d",&x);
begin=0;end=N-1;
if(x>a[0]||x<a[N-1])
printf("error");
else
while(begin<=end)
{
i = (begin+end) / 2;
if (x==a[i])
{
y=i+1;
printf("%d",y);
break;
}
else
if (x>a[i])
end=i-1;
else
begin=i+1;
}
printf("\ncontinue?y/n\n");
scanf("%c",&c);
if(c!='y') flag=0;
}
getch();
return 0;
}
为什么
scanf("%c",&c);
if(c!='y') flag=0;
不起作用?不论输入什么显示窗口都关闭了?