#include <stdio.h>
int main (void)
{
int guess = 1;
int i = 0;
int total = 50;
int k = 0;
int j = 50;
char ch;
printf("Pick an integer from 1 to 100. I will try to guess ") ;
printf ("it. \nRespond with a c if my guess is right \n") ;
printf("is bigger than %d?\n",total);
while ((ch=getchar())!= 'c')
{
while((getchar())!='\n')
continue; //当直接输入两个回车时,程序依然是可执行的.
if(ch=='y'||ch=='n')//这里是关键!把下面一列的的处理都包含在输入正确的前提上
{
if(j!=1)
{
j=j/2;
if(j==0)j=1;
i=total-j;
k=total+j;
printf("ch=%c,total=%d,j=%d,i=%d,k=%d\n",ch,total,j,i,k);
if (ch == 'y')
{
total= k;
printf("is bigger than %d?\n",total);
}
else if(ch == 'n')
{
total= i;
printf("is bigger than %d?\n",total);
}
}
else
{
if (ch == 'y')
{
total= total+1;
printf("is bigger than %d?\n",total);
}
else if(ch == 'n')
{
total= total-1;
printf("is bigger than %d?\n",total);
}
}
}
else continue;//如果最开始的输入是错误的就直接进行下一次循环
}
printf("I knew I could do it! \n") ;
return 0;
}
题解 - 序列
2024-01-02 19:40:01 116 阅读