spoj1026 favorite dice

本文探讨了一个数学问题,即求解投掷N面骰子直至每一面至少出现一次所需的平均次数。通过建立递推公式,文章提供了一种算法解决方案,并附带了C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意翻译

一个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.

 

输入输出样例

输入样例#1:

2
1
12

输出样例#1:

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 ]挪到等式的一侧就可以了)

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int T;
double f[1003];
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		int n;
		scanf("%d",&n);
		memset(f,0,sizeof(f));
		f[n] = 0;
		for(int i = n - 1;i >= 0;i--)
		{
			f[i] = f[i + 1] + n/(n - (double)i);
		}
		printf("%.2lf\n",f[0]);
	}
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值