c语言中程序循环
#include "stdio.h"#include "stdlib.h"
int main()
{
int a[15],i,mid,top=0,number,flag=1,botte=14,t,m;
char c=0;
scanf("%d",&a[0]);
for(i=1;i<15;i++)
{
scanf("%d",&a[i]);
if(a[i]<=a[i-1])
{i--;
printf("input this number again:\n");
}
}
printf("\n");
for(i=0;i<15;i++)
printf("%-4d",a[i]);
printf("\n");
while(flag)
{
t=0;
m=1;
printf("input the number you search:\n");
scanf("%d",&number);
if(number<a[0]||number>a[14])
m=0;
while(top<=botte&&t!=1)
{
mid=(top+botte)/2;
if(a[mid]==number)
{
printf("has found %d,its position is %d\n",number,mid+1);
t=1;
}
else if(a[mid]>number)
botte=mid+1;
else top=mid-1;
}
if(m==0||t!=1)
printf("cant find %d\n",number);
printf("是否继续查找y/n\n");
scanf("%c",&c);
if(c=='N'||c=='n') flag=0;
}
system("pause");
return 0;
}
为什么不能执行它的循环搜索功能,错在哪里?
追问:来自手机问问我这个mid不能去啊,要实现分半法搜索的思想啊