把 n 个骰子仍在地上,求点数和为 s 的概率。
java:
1 public List<Map.Entry<Integer, Double>> dicesSum(int n) { 2 final int face = 6; 3 final int pointNum = face * n; 4 long[][] dp = new long[n + 1][pointNum + 1]; 5 6 for (int i = 1; i <= face; i++) 7 dp[1][i] = 1; 8 9 for (int i = 2; i <= n; i++) 10 for (int j = i; j <= pointNum; j++) /* 使用 i 个骰子最小点数为 i */ 11 for (int k = 1; k <= face && k <= j; k++) 12 dp[i][j] += dp[i - 1][j - k]; 13 14 final double totalNum = Math.pow(6, n); 15 List<Map.Entry<Integer, Double>> ret = new ArrayList<>(); 16 for (int i = n; i <= pointNum; i++) 17 ret.add(new AbstractMap.SimpleEntry<>(i, dp[n][i] / totalNum)); 18 19 return ret; 20 }