hdu 3750 Guess Game

hdu 3750 Guess Game

Problem Description
Bob plays the “guess the number right” with Alice recently,the game’s rule is that Alice give Bob a upper limit number N ,then he write any of a number on paper which Bob can’t know what it is and the number must be between 1 and N.Bob has many chances to guess the number and every time when Bob guesses Alice will tell him if his is bigger than the correct number or small than the correct number until he is right.
Now the Bob wanted to use binary search method to guess the right number, because he knows this method is quite fast to find the right number.

Input
We test the problem in many cases.Each case begin with one integers N( 1<= N <= 100000 ).

Output
Output the expected number of chances required to guess the number right, which accurate to 2 fractional digits.

Sample Input
2
3

Sample Output
1.50
1.67

题目大意:给你一个数字n,从一到n,用二分的方法去猜其中一个数,问你猜中的几率有多大。

思路:其实猜中的概率总和都可以写成1+2*2^1+3*2^2+4*2^3+…./n
注意保留两位小数就可以了。

#include<bits/stdc++.h>
using namespace std;
int a[101];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int j=1;
        long long int sum=0;

        for(int i=1;;i++)
        {
            if(pow(2,j)-1<=n)
            {
                sum+=j*pow(2,j-1);
                j++;
            }
            else
            {
                sum+=(n-pow(2,j-1)+1)*j;
                break;
            }
        }
        printf("%.2lf\n",sum*1.0/n);
    }
    return 0;
}

代码提交:http://acm.hdu.edu.cn/showproblem.php?pid=3750

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值