Home > Back-end >  Twice in circulation control problem: why "Is the number 75? Y or N?"
Twice in circulation control problem: why "Is the number 75? Y or N?"

Time:04-07

 # include & lt; stdio.h> 
# define MIN 0
# define MAX 100
Int main (void)
{
Int number=(MIN and MAX)/2;
Char c='N';
Int compare;

Printf (" Let me guess the number. \ n ");
While (c! )
='Y'{
Printf (" Is the number % d? Y/N? \ n ", number);
The scanf (" % c ", & amp; C);
If (c=='Y')
break;//the number of desired value
If (c=='N')
{
Printf (" 1) bigger 2) smaller \ n both Please choose 1 or 2! \n");
The scanf (" % d ", & amp; Compare);
If (compare==1)
Number=(number + MAX)/2;
If (compare==2)
Number=(number + MIN)/2;
}
}
Printf (".so, the number is % d. \ n ", number).

return 0;
}


Let me guess the number.
Is the number 50? Y/N?
N
1) the bigger 2) smaller
Both Please choose 1 or 2!
1
Is the number 75? Y/N?
Is the number 75? Y/N?
Y
So, the number is 75.


Why appears twice "Is the number 75? Y or N?"

CodePudding user response:

Reference:
 # include & lt; stdio.h> 
# define MIN 0
# define MAX 100
Int main (void)
{
Int number=(MIN and MAX)/2;
Char c='N';
Int compare;

Printf (" Let me guess the number. \ n ");
While (c! )
='Y'{
Printf (" Is the number % d? Y/N? \ n ", number);
The rewind (stdin);//remove the standard input key buffer
The scanf (" % c ", & amp; C);
If (c=='Y')
break;//the number of desired value
If (c=='N')
{
Printf (" 1) bigger 2) smaller \ n both Please choose 1 or 2! \n");
The scanf (" % d ", & amp; Compare);
If (compare==1)
Number=(number + MAX)/2;
If (compare==2)
Number=(number + MIN)/2;
}
}
Printf (".so, the number is % d. \ n ", number).

return 0;
}
  • Related