2019-11-02 12:07:41 +08:00
|
|
|
|
# 60. n 个骰子的点数
|
|
|
|
|
|
2019-11-03 23:57:08 +08:00
|
|
|
|
## 题目链接
|
|
|
|
|
|
2019-11-02 12:07:41 +08:00
|
|
|
|
[Lintcode](https://www.lintcode.com/en/problem/dices-sum/)
|
|
|
|
|
|
|
|
|
|
## 题目描述
|
|
|
|
|
|
|
|
|
|
把 n 个骰子扔在地上,求点数和为 s 的概率。
|
|
|
|
|
|
2019-11-02 17:33:10 +08:00
|
|
|
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/195f8693-5ec4-4987-8560-f25e365879dd.png" width="300px"> </div><br>
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
## 解题思路
|
|
|
|
|
|
|
|
|
|
### 动态规划
|
|
|
|
|
|
2019-11-03 23:57:08 +08:00
|
|
|
|
使用一个二维数组 dp 存储点数出现的次数,其中 dp\[i]\[j] 表示前 i 个骰子产生点数 j 的次数。
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
空间复杂度:O(N<sup>2</sup>)
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public List<Map.Entry<Integer, Double>> dicesSum(int n) {
|
|
|
|
|
final int face = 6;
|
|
|
|
|
final int pointNum = face * n;
|
|
|
|
|
long[][] dp = new long[n + 1][pointNum + 1];
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= face; i++)
|
|
|
|
|
dp[1][i] = 1;
|
|
|
|
|
|
|
|
|
|
for (int i = 2; i <= n; i++)
|
|
|
|
|
for (int j = i; j <= pointNum; j++) /* 使用 i 个骰子最小点数为 i */
|
|
|
|
|
for (int k = 1; k <= face && k <= j; k++)
|
|
|
|
|
dp[i][j] += dp[i - 1][j - k];
|
|
|
|
|
|
|
|
|
|
final double totalNum = Math.pow(6, n);
|
|
|
|
|
List<Map.Entry<Integer, Double>> ret = new ArrayList<>();
|
|
|
|
|
for (int i = n; i <= pointNum; i++)
|
|
|
|
|
ret.add(new AbstractMap.SimpleEntry<>(i, dp[n][i] / totalNum));
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 动态规划 + 旋转数组
|
|
|
|
|
|
|
|
|
|
空间复杂度:O(N)
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public List<Map.Entry<Integer, Double>> dicesSum(int n) {
|
|
|
|
|
final int face = 6;
|
|
|
|
|
final int pointNum = face * n;
|
|
|
|
|
long[][] dp = new long[2][pointNum + 1];
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= face; i++)
|
|
|
|
|
dp[0][i] = 1;
|
|
|
|
|
|
|
|
|
|
int flag = 1; /* 旋转标记 */
|
|
|
|
|
for (int i = 2; i <= n; i++, flag = 1 - flag) {
|
|
|
|
|
for (int j = 0; j <= pointNum; j++)
|
|
|
|
|
dp[flag][j] = 0; /* 旋转数组清零 */
|
|
|
|
|
|
|
|
|
|
for (int j = i; j <= pointNum; j++)
|
|
|
|
|
for (int k = 1; k <= face && k <= j; k++)
|
|
|
|
|
dp[flag][j] += dp[1 - flag][j - k];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final double totalNum = Math.pow(6, n);
|
|
|
|
|
List<Map.Entry<Integer, Double>> ret = new ArrayList<>();
|
|
|
|
|
for (int i = n; i <= pointNum; i++)
|
|
|
|
|
ret.add(new AbstractMap.SimpleEntry<>(i, dp[1 - flag][i] / totalNum));
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-02 17:33:10 +08:00
|
|
|
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|