comments | difficulty | edit_url |
---|---|---|
true |
简单 |
把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
我们定义
其中
初始条件为
最终,我们要求的答案即为
时间复杂度
class Solution:
def dicesProbability(self, n: int) -> List[float]:
f = [[0] * (6 * n + 1) for _ in range(n + 1)]
for j in range(1, 7):
f[1][j] = 1
for i in range(2, n + 1):
for j in range(i, 6 * i + 1):
for k in range(1, 7):
if j - k >= 0:
f[i][j] += f[i - 1][j - k]
m = pow(6, n)
return [f[n][j] / m for j in range(n, 6 * n + 1)]
class Solution {
public double[] dicesProbability(int n) {
int[][] f = new int[n + 1][6 * n + 1];
for (int j = 1; j <= 6; ++j) {
f[1][j] = 1;
}
for (int i = 2; i <= n; ++i) {
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j >= k) {
f[i][j] += f[i - 1][j - k];
}
}
}
}
double m = Math.pow(6, n);
double[] ans = new double[5 * n + 1];
for (int j = n; j <= 6 * n; ++j) {
ans[j - n] = f[n][j] / m;
}
return ans;
}
}
class Solution {
public:
vector<double> dicesProbability(int n) {
int f[n + 1][6 * n + 1];
memset(f, 0, sizeof f);
for (int j = 1; j <= 6; ++j) {
f[1][j] = 1;
}
for (int i = 2; i <= n; ++i) {
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j >= k) {
f[i][j] += f[i - 1][j - k];
}
}
}
}
vector<double> ans;
double m = pow(6, n);
for (int j = n; j <= 6 * n; ++j) {
ans.push_back(f[n][j] / m);
}
return ans;
}
};
func dicesProbability(n int) (ans []float64) {
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, 6*n+1)
}
for j := 1; j <= 6; j++ {
f[1][j] = 1
}
for i := 2; i <= n; i++ {
for j := i; j <= 6*i; j++ {
for k := 1; k <= 6; k++ {
if j >= k {
f[i][j] += f[i-1][j-k]
}
}
}
}
m := math.Pow(6, float64(n))
for j := n; j <= 6*n; j++ {
ans = append(ans, float64(f[n][j])/m)
}
return
}
/**
* @param {number} n
* @return {number[]}
*/
var dicesProbability = function (n) {
const f = Array.from({ length: n + 1 }, () => Array(6 * n + 1).fill(0));
for (let j = 1; j <= 6; ++j) {
f[1][j] = 1;
}
for (let i = 2; i <= n; ++i) {
for (let j = i; j <= 6 * i; ++j) {
for (let k = 1; k <= 6; ++k) {
if (j >= k) {
f[i][j] += f[i - 1][j - k];
}
}
}
}
const ans = [];
const m = Math.pow(6, n);
for (let j = n; j <= 6 * n; ++j) {
ans.push(f[n][j] / m);
}
return ans;
};
public class Solution {
public double[] DicesProbability(int n) {
int[,] f = new int[n + 1, 6 * n + 1];
for (int j = 1; j <= 6; ++j) {
f[1, j] = 1;
}
for (int i = 2; i <= n; ++i) {
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j >= k) {
f[i, j] += f[i - 1, j - k];
}
}
}
}
double m = Math.Pow(6, n);
double[] ans = new double[5 * n + 1];
for (int j = n; j <= 6 * n; ++j) {
ans[j - n] = f[n, j] / m;
}
return ans;
}
}
class Solution {
func dicesProbability(_ n: Int) -> [Double] {
var f = Array(repeating: Array(repeating: 0, count: 6 * n + 1), count: n + 1)
for j in 1...6 {
f[1][j] = 1
}
if n > 1 {
for i in 2...n {
for j in i...(6 * i) {
for k in 1...6 {
if j >= k {
f[i][j] += f[i - 1][j - k]
}
}
}
}
}
var m = 1.0
for _ in 0..<n {
m *= 6.0
}
var ans = Array(repeating: 0.0, count: 5 * n + 1)
for j in n...(6 * n) {
ans[j - n] = Double(f[n][j]) / m
}
return ans
}
}
我们可以发现,上述方法中的
class Solution:
def dicesProbability(self, n: int) -> List[float]:
f = [0] + [1] * 6
for i in range(2, n + 1):
g = [0] * (6 * i + 1)
for j in range(i, 6 * i + 1):
for k in range(1, 7):
if 0 <= j - k < len(f):
g[j] += f[j - k]
f = g
m = pow(6, n)
return [f[j] / m for j in range(n, 6 * n + 1)]
class Solution {
public double[] dicesProbability(int n) {
int[] f = new int[7];
Arrays.fill(f, 1);
f[0] = 0;
for (int i = 2; i <= n; ++i) {
int[] g = new int[6 * i + 1];
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j - k >= 0 && j - k < f.length) {
g[j] += f[j - k];
}
}
}
f = g;
}
double m = Math.pow(6, n);
double[] ans = new double[5 * n + 1];
for (int j = n; j <= 6 * n; ++j) {
ans[j - n] = f[j] / m;
}
return ans;
}
}
func dicesProbability(n int) (ans []float64) {
f := make([]int, 7)
for i := 1; i <= 6; i++ {
f[i] = 1
}
for i := 2; i <= n; i++ {
g := make([]int, 6*i+1)
for j := i; j <= 6*i; j++ {
for k := 1; k <= 6; k++ {
if j-k >= 0 && j-k < len(f) {
g[j] += f[j-k]
}
}
}
f = g
}
m := math.Pow(6, float64(n))
for j := n; j <= 6*n; j++ {
ans = append(ans, float64(f[j])/m)
}
return
}
/**
* @param {number} num
* @return {number[]}
*/
var dicesProbability = function (n) {
let f = Array(7).fill(1);
f[0] = 0;
for (let i = 2; i <= n; ++i) {
let g = Array(6 * i + 1).fill(0);
for (let j = i; j <= 6 * i; ++j) {
for (let k = 1; k <= 6; ++k) {
if (j - k >= 0 && j - k < f.length) {
g[j] += f[j - k];
}
}
}
f = g;
}
const ans = [];
const m = Math.pow(6, n);
for (let j = n; j <= 6 * n; ++j) {
ans.push(f[j] / m);
}
return ans;
};
public class Solution {
public double[] DicesProbability(int n) {
int[] f = new int[7];
for (int i = 1; i <= 6; ++i) {
f[i] = 1;
}
f[0] = 0;
for (int i = 2; i <= n; ++i) {
int[] g = new int[6 * i + 1];
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j - k >= 0 && j - k < f.Length) {
g[j] += f[j - k];
}
}
}
f = g;
}
double m = Math.Pow(6, n);
double[] ans = new double[5 * n + 1];
for (int j = n; j <= 6 * n; ++j) {
ans[j - n] = f[j] / m;
}
return ans;
}
}