题意翻译
一个n面的骰子,求期望掷几次能使得每一面都被掷到。
题目描述
BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:
What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?
输入输出格式
输入格式:
The first line of the input contains an integer t, the number of test cases. t test cases follow.
Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.
输出格式:
For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.
输入输出样例
2
1
12
1.00
37.24
f [ i ]表示还剩i个面能把骰子的n面全扔一遍
对于扔一次骰子,有(n - i)/n能扔到剩下的面,有扔到之前扔过的面
f [ i ] = f [i + 1] * (( n - i ) / n ) + ( i / n) * f [ i ] + 1;
化简可得到f[i] = f [i + 1] + n/(n - i);(把f[ i ]挪到等式的一侧就可以了)
---------------------
作者:anonymity__
来源:CSDN
原文:https://blog.csdn.net/qq_42914224/article/details/83889581
版权声明:本文为博主原创文章,转载请附上博文链接!
代码是我自个的
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 int t; 7 double f[1005]; 8 int main() 9 { 10 scanf("%d",&t); 11 while(t--) 12 { 13 int n; 14 scanf("%d",&n); 15 memset(f,0,sizeof(f)); 16 f[n] = 0; 17 for(int i = n - 1;i >= 0;i--) 18 { 19 f[i] = f[i + 1] + n / (n - (double)i); 20 } 21 printf("%0.2lf ",f[0]); 22 } 23 return 0; 24 }