hdu 3750 Guess Game
来源:互联网 发布:武田毅雄 知乎 编辑:程序博客网 时间:2024/06/05 03:45
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
- hdu Guess Game 3750
- hdu 3750 Guess Game
- HDU-3750-Guess Game
- hdu 3750 Guess Game
- 【期望】 HDU 3750 Guess Game
- hdu 3750 Guess Game 数学期望
- hdoj 3750 Guess Game 【数学之二分】
- TOJ 4101 Guess Game
- Guess Number Game
- lintcode:Guess Number Game
- lintcode(662)Guess Number Game
- lintcode Guess Number Game(Java)
- LintCode Guess Number Game II
- lintcode Guess Number Game II
- Mini-project # 2 - "Guess the number" game"
- lintcode(666)Guess Number Game II
- Google/LintCode:E-Guess Number Game
- game——Guess the number!
- ACM-大数N!的位数公式
- Django的学习笔记(一)(Django Web入门)
- Hibernate入门(三)之hibernate 的session的缓存机制
- UserDefaults 和 sandbox
- 设计模式学习笔记--桥接模式
- hdu 3750 Guess Game
- android项目之通讯录数据库
- Ugly Number(丑数)系列题 Python解法
- JavaScript-观察者模式(publish/subscribe)
- 挂载u盘
- HDU 1068 Girls and Boys(二分匹配--匈牙利算法)
- 使用友盟来实现第三方的分享,登陆
- 工具栏(JToolBar)
- Dubbo源码分析(十):服务发布