(C语言)二分算法实现简单猜数
1#include <stdio.h>
2int guessnum(int,int);
3char correntchar(void);
4int main(void)
5{
6 char ch;
7 int min=1;int max=100;
8 int value;
9 printf("Pick a integer form 1 to 100,then I will try to guess it:");
10 printf("\nSome answers you can choose to help me,as follows:");
11 printf("\nb bigger than my answer s smaller");
12 printf("\ny corrent.");
13 value=guessnum(min,max);
14 printf("\nIs it %d?",value);
15
16 while((ch=correntchar())!='y')
17 {
18 //while(getchar())
19 if(ch=='b')
20 min=value;
21 else if(ch=='s')
22 max=value;
23 value=guessnum(min,max);
24 printf("\nIs it %d?",value);
25 }
26 printf("Oh,year!");
27 return 0;
28}
29int guessnum(int min,int max)
30{
31 return (max+min)/2;
32}
33char correntchar()
34{
35 char ch;
36 ch=getchar();
37 while(getchar()!='\n')
38 continue;
39 return ch;
40}
2int guessnum(int,int);
3char correntchar(void);
4int main(void)
5{
6 char ch;
7 int min=1;int max=100;
8 int value;
9 printf("Pick a integer form 1 to 100,then I will try to guess it:");
10 printf("\nSome answers you can choose to help me,as follows:");
11 printf("\nb bigger than my answer s smaller");
12 printf("\ny corrent.");
13 value=guessnum(min,max);
14 printf("\nIs it %d?",value);
15
16 while((ch=correntchar())!='y')
17 {
18 //while(getchar())
19 if(ch=='b')
20 min=value;
21 else if(ch=='s')
22 max=value;
23 value=guessnum(min,max);
24 printf("\nIs it %d?",value);
25 }
26 printf("Oh,year!");
27 return 0;
28}
29int guessnum(int min,int max)
30{
31 return (max+min)/2;
32}
33char correntchar()
34{
35 char ch;
36 ch=getchar();
37 while(getchar()!='\n')
38 continue;
39 return ch;
40}