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;
}
本文介绍了一道名为“Guess Game”的编程题,该题要求使用二分查找算法来猜测由1到N之间的某个未知数字,并计算出猜中所需平均次数。文章给出了详细的解题思路和实现代码。
1102

被折叠的 条评论
为什么被折叠?



