Seeing that all the solutions are backwards, but none of them are forward-thinking, let’s talk about it here.
Setting fi f_ifiIndicates that ii has been rolledThe expected number of times when i different faces are
initializedf 0 = 0 f_0=0f0=0
Then, the transfer equation is obviouslyfi = n − i + 1 n ⋅ fi − 1 + n − in ⋅ fi + 1 f_i = \frac{n-i+1}{n} \cdot f_{i-1}+ \frac{ni}{n} \cdot f_i +1fi=nn−i+1⋅fi−1+nn−i⋅fi+1
化简后得到 f i = f i − 1 × ( n − i + 1 ) + n n − i f_i=\frac{f_{i-1}\times(n-i+1)+n}{n-i} fi=n−ifi−1×(n−i+1)+n
It can be found that if this is recursive to nnWhen n , the denominator is0 00 , so this method is not feasible
So, we try to use backwards
Setting fi f_ifiIndicates that ii has been rolledAfter i different faces, how many more times are expected to reachnnn faces. Initializefn = 0 f_n=0fn=0
转移方程 f i = n − i n ⋅ f i + 1 + i n ⋅ f i + 1 f_i=\frac{n-i}{n} \cdot f_{i+1}+\frac{i}{n} \cdot f_i+1 fi=nn−i⋅fi+1+ni⋅fi+1
化简后得 f i = f − i + 1 + n n − i f_i=f-{i+1}+\frac{n}{n-i} fi=f−i+1+n−in
In this way, there will be no denominator of 0 0In the case of 0 , the answer isf 0 f_0f0
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int Maxn=1010;
double f[Maxn];
int n,T;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
f[n]=0.0;
for(int i=n-1;i>=0;--i)
f[i]=f[i+1]+(n*1.0)/((n-i)*1.0);
printf("%.2lf\n",f[0]);
}
return 0;
}