/*修改程序清单8.4中的猜测程序,使其使用更智能的猜测策略。使用二分搜索策略*/
#include <stdio.h>
int main(void)
{
char ch;
int guess = 50;
int min = 1;
int max = 100;
printf("pick an integer from 1 to 100. I will try to gusse ");
printf("it.\nRespond with a y or b or s (s mines small, b mines big, y mines yes) ");
printf("Uh...is your number %d?\n", guess);
while ((ch = getchar()) != 'y')
{
if (ch == 'b')
{
max = guess;
guess = (max + min)/2;
printf("well then is %d?\n", guess);
}
if (ch == 's')
{
min = guess;
guess = (max + min)/2;
printf("well then is %d?\n", guess);
}
if (ch != 's' && ch != 'b' && ch != 'y')
printf("sorry, I understand only y or s or b.\n");
while ((ch = getchar())!= '\n')
continue;
}
printf("I know I could do it!\n");
return 0;
}修改程序清单8.4中的猜测程序,使其使用更智能的猜测策略。使用二分搜索策略。
最新推荐文章于 2023-07-30 21:09:14 发布
本文介绍了一个使用二分搜索策略实现的猜数游戏程序。该程序能够在用户设定的1到100之间的数字范围内,通过不断缩小范围来猜测用户心中所想的数字。文章详细展示了如何根据用户的反馈调整猜测策略。

745

被折叠的 条评论
为什么被折叠?



