LightOJ 1408 Batting Practice (期望)
题目连接:http://lightoj.com/volume\_showproblem.php?problem=1408
题意:连续进k1个球或连续不进k2个球则游戏结束,给出进球概率p,求到游戏结束时投球个数的期望。
思路:f[i]表示连续i次不命中时到游戏结束剩余投球个数的期望,t[i]表示连续i次命中时到游戏结束剩余投球个数的期望。
设命中概率p,则不命中q=1-p。f[i] = q*(f[i+1]+1)+p*(1+t[1]) , t[i] = p*(t[i+1]+1)+q*(1+f[1]).
边界:f[n]=t[m]=0。
解出f[1],t[1],则答案为f[0]=t[0]=p*t[1]+q*f[1]+1.
高中时解方程的准确率已经完全木有了。。。。
#include <cstdio>
#include <cmath>
const double STD=1e-10;
int main ()
{
#ifdef ONLINE_JUDGE
#else
freopen("read.txt","r",stdin);
#endif
int T,k1,k2;
double p,q;
scanf("%d",&T);
for (int Cas=1;Cas<=T;Cas++)
{
scanf("%lf%d%d",&p,&k1,&k2);
q=1-p;
if (p>1-STD)
{
printf("Case %d: %lf\n",Cas,1.0*k2);
continue;
}
if (p<STD)
{
printf("Case %d: %lf\n",Cas,1.0*k1);
continue;
}
double a=1-pow(q,k1-1);
double b=1-pow(p,k2-1);
double x=(a*b/q+a/p)/(1-a*b),y=b*x+b/q;
printf("Case %d: %lf\n",Cas,p*y+q*x+1);
}
return 0;
}
还没有评论,来说两句吧...