(C语言)二分算法实现简单猜数

 1 None.gif #include  < stdio.h >
 2 None.gif int  guessnum( int , int );
 3 None.gif char  correntchar( void );
 4 None.gif int  main( void )
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    char ch;
 7InBlock.gif    int min=1;int max=100;
 8InBlock.gif    int value;
 9InBlock.gif    printf("Pick a integer form 1 to 100,then I will try to guess it:");
10InBlock.gif    printf("\nSome answers you can choose to help me,as follows:");
11InBlock.gif    printf("\nb bigger than my answer    s smaller");
12InBlock.gif    printf("\ny corrent.");
13InBlock.gif    value=guessnum(min,max);
14InBlock.gif    printf("\nIs it %d?",value);
15InBlock.gif
16InBlock.gif    while((ch=correntchar())!='y')
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            //while(getchar())
19InBlock.gif                if(ch=='b')
20InBlock.gif                    min=value;
21InBlock.gif                else if(ch=='s')
22InBlock.gif                    max=value;
23InBlock.gif                value=guessnum(min,max);
24InBlock.gif                printf("\nIs it %d?",value);
25ExpandedSubBlockEnd.gif        }

26InBlock.gif    printf("Oh,year!");
27InBlock.gif    return 0;
28ExpandedBlockEnd.gif}

29 None.gif int  guessnum( int  min, int  max)
30 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
31InBlock.gif    return (max+min)/2;
32ExpandedBlockEnd.gif}

33 None.gif char  correntchar()
34 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
35InBlock.gif    char ch;
36InBlock.gif    ch=getchar();
37InBlock.gif    while(getchar()!='\n')
38InBlock.gif        continue;
39InBlock.gif    return ch;
40ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/feiren1421/archive/2006/02/26/337881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值