HDU3750-猜数概率期望

Guess Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 566    Accepted Submission(s): 332


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
 

Author
erriyue
 

Source
 

Recommend
lcy
 
#include<iostream>
#include<cstdio>
using namespace std;
double binary_search(int key,int n)
{
	double ans=0;
	int l=1,r=n;
	int mid;
	while(l<=r)
	{
		ans+=1;
		mid=(l+r)/2;
		if(mid==key)
			break;
		if(mid>key)
			r=mid-1;
		else 
			l=mid+1;
	}
	return ans;
}
int main()
{
	double N;
	while(cin>>N)
	{
		double count=0;
		for(int i=1;i<=N;i++)//直接枚举每个可能的值,加上每次算的次数,总次数/可能的个数N==期望
			count+=binary_search(i,N);//得到每个值的期望 的总和。
		printf("%.2lf\n",count/N);  //然后平均分!!!就得到了猜一个数平均的期望。
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值