题目链接:传送门
题意:
初始状态在0,每次丢骰子[1,6],如果现在在x,丢的骰子数为y,如果x+y>n那么就还停留在x。
求从0到n所丢骰子次数的期望。
分析:
这题和之前的就更改了一点就是如果现在在x,丢的骰子数为y,如果x+y>n那么就还停留在x。
那么我们设dp[i]表示从i到n要丢的骰子次数的期望
那么
我们设每次有x次的可能留在原地
dp[i] =dp[i]*y/6+dp[i+1]/6+dp[i+2]/6..+1;
然后化简一下得到dp[i]的公式。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
double dp[maxn];
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i=n-1;i>=0;i--){
int tot=0;
double tmp=0;
for(int j=1;j<=6;j++){
if(i+j>n) tot++;
else tmp+=dp[i+j]/6.0;
}
//cout<<"tot "<<tot<<endl;
dp[i]=(tmp+1)/(6-tot)*6;
}
printf("%.2lf\n",dp[0]);
}
return 0;
}