HDU-3750-Guess Game
2016-05-07 19:34
435 查看
HDU-3750-Guess Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题目链接:HDU-3750
题目大意:猜数字,给出一个数字在[1,n]范围内,Bob采用二分的方法猜,问才对的期望
题目思路:每个数字的可能性为1/n,所以只要求出猜到第i个数字需要几次记为xi,把(x1 + x2 + x3 + …..xn)* 1 / n 就是答案。可用递归求出各个xi
以下是代码:
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #include<iomanip> using namespace std; typedef long long ll; int mp[100005]; void solve(int l,int r,int pos) { if (l > r) return; if (l == r) { if (mp[l] == 0) mp[l] = pos; return; } int mid = (l + r) / 2; mp[mid] = pos; pos++; solve(l,mid - 1,pos); solve(mid + 1,r,pos); } int main() { int n; double ret; while (cin >> n) { memset(mp,0,sizeof(mp)); ret = 1.0 / n; solve(1,n,1); long long ans = 0; for (int i = 1; i <= n; i++) { ans += mp[i]; } printf("%.2f\n",ans * ret); } return 0; }
相关文章推荐
- 最长公共子序列LCS(The longest common subsequence)
- 【UNET自学日志】Part20 UI
- 关键字提取工具(面向英文)
- 最长上升子序列LIS(Longest Increasing Subsequence)
- mysql5中大数据错误:Packet for query is too large (***>***). You can change this value on the ser
- mysql 5.5 开启慢日志slow log的方法(log_slow_queries)
- MySQL开启慢查询日志log-slow-queries的方法
- 蓝牙(Bluetooth)
- Servlet中使用RequestDispatcher调派请求--forware
- Android Volley完全解析(三),定制自己的Request
- 2016 UESTC ACM Summer Training Team Selection (2)解题报告
- System and Device Programming------questions solved about device
- HDU 1711 Number Sequence (KMP)
- Servlet中使用RequestDispatcher调派请求--include
- 【转】SVN的UUID错误
- Java ArrayDeque源码剖析
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor
- iOS UISearchController使用
- UGUI 实现屏幕外怪物的指示箭头
- 自从我整理好了它?UI在也不敢说咋们程序员没审美!