源代码:
- 1 #include <stdio.h>
- 2 int main(void)
- 3 {
- 4 int max = 100;
- 5 int min = 1;
- 6 int guess = ( max + min ) / 2;
- 7 char response;
- 8 printf("%d,large, small or yes?Enter(l|s|y):",guess);
- 9 while((response = getchar()) != 'y')
- 10 {
- 11 if(response == 's')
- 12 {
- 13 max = guess;
- 14 guess = ( max + min ) / 2;
- 15 }
- 16 else if(response == 'l')
- 17 {
- 18 min = guess;
- 19 guess = ( max + min ) / 2;
- 20 }
- 21 else
- 22 {
- 23 printf("Input error!");
- 24 }
- 25 while(getchar() != '\n')
- 26 continue;
- 27 printf("%d,large, small or yes?Enter(l|s|y):",guess);
- 28 }
- 29 printf("Your guess number is %d\n",guess);
- 30 return 0;
- 31 }
运行效果:
:!gcc test5.c -o test5 :! ./test5 50,large, small or yes?Enter(l|s|y):l 75,large, small or yes?Enter(l|s|y):s 62,large, small or yes?Enter(l|s|y):l 68,large, small or yes?Enter(l|s|y):s 65,large, small or yes?Enter(l|s|y):l 66,large, small or yes?Enter(l|s|y):y Your guess number is 66 |