面试题60
把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。
你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。
限制:
1 <= n <= 11
Solutions
dynamic programming
Count the number of cases with sum equals to values(sums) ranging from
n
ton * 6
after rolledn
times.Then the propability of each
value(sum)
iscount * pow(1.0/6, n)
. ie: totoal number of combinations is6^n
dp[n][i]
represents the number of cases with sum equals toi
after we rolledn
times.Then
dp[n + 1][i] = dp[n][i - 6](current dice is 6) + dp[n][i - 5](current dice is 5) + .... dp[n][i - 1]
.
Last updated
Was this helpful?