UVa 10900 So you want to be a 2n-aire?

99 篇文章 0 订阅
42 篇文章 0 订阅

The player starts with a prize of $1,and is asked a sequence of n questions.For each question, he may• quit and keep his prize.• answer the question. Ifwrong, he quits with nothing.If correct, the prize is doubled,and he continues withthe next question.After the last question, he quitswith his prize. The player wants tomaximize his expected prize.Once each question is asked, theplayer is able to assess the probabilityp that he will be able to answerit. For each question, we assume that p is a random variable uniformly distributed over the range t..1.

Input

Input is a number of lines, each with two numbers: an integer 1 ≤ n ≤ 30, and a real 0 ≤ t ≤ 1. Inputis terminated by a line containing ‘0 0’. This line should not be processed.

Output

For each input n and t, print the player’s expected prize, if he plays the best strategy. Output shouldbe rounded to three fractional digits.

Sample Input

1 0.5

1 0.3

2 0.6

24 0.25

0 0

Sample Output

1.500

1.357

2.560

230.138

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

概率+神奇的思路~

设d[i]为第i步的时候的期望钱数。

因为d[i]会受到i之后的d的影响,所以要倒推;而每次答对的概率只有一个范围,所以要先判断范围内哪些概率要答题,那些不需要答题,然后计算就可以了。

哪里用double哪里用int一定要分清!!!

(CSDN又不能给自己点赞了,差评。)


#include<cstdio>
#include<iostream>
using namespace std;

int n,num[31];
double p,d[31];

int main()
{
	num[0]=1;
	for(int i=1;i<=30;i++) num[i]=num[i-1]*2;
	while(scanf("%d%lf",&n,&p)!=EOF && n)
	{
		d[n]=num[n];
		for(int i=n-1;i>=0;i--)
		{
			double p0=max(p,num[i]/d[i+1]),p1=(p0-p)/(1-p);
			d[i]=p1*num[i];
			d[i]+=d[i+1]*(1+p0)/2*(1-p1);
		}
		printf("%.3lf\n",d[0]);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值