1个回答
展开全部
与上述引用同一种问题。
//二分法查找数据,1-100内查找一个数据,查找一次需要判断一下Yes or No.
#include<stdio.h>
int main()
{
//初始化
int low = 1, high = 100, guess, mid = low, k = 0;
char a = 'n';
//主体
printf("Please input integer from 1 to 100,I will try to guess it.\n ");
scanf("%d", &guess);
printf("If i guess true input 'y',else input 'n'.\n\n");
while (1)
{
printf("测试循环了 %d 次\n",k++);
mid = (low + high) / 2;
printf("Um...is your number is %d ?\n", mid);
getchar(); //作用:“吃掉”‘\n’,否则下一行的scanf函数会读入回车符号。
scanf("%c/n", &a);
if (a != 'y')
{
if (mid < guess) //猜测数在mid--high之间
{
low = mid;
}
else if (mid > guess) //猜测数在low--mid之间
{
high = mid;
}
}
else break;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询