面试题60. n个骰子的点数(一般)
把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。
你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。
示例 1:
输入: 1
输出: [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]
示例 2:
输入: 2
输出: [0.02778,0.05556,0.08333,0.11111,0.13889,0.16667,0.13889,0.11111,0.08333,0.05556,0.02778]
限制:
1 <= n <= 11
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof
思路:
动态规划思路:
dp[i] [j]表示掷i个骰子,出现j点数的次数
转移方程为
dp[i] [s] += dp[i-1] [s-j];//转移方程 ,当前n个骰子出现的点数之和等于前一次出现的点数之和加上这一次出现的点数
代码1:
1 | class Solution { |