剑指 Offer 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
class Solution {
public:
vector<double> dicesProbability(int n) {
vector<double> dp(6, 1.0 / 6.0);//先将它们概率初始为1/6
for(int i = 2; i <= n; i++){
vector<double> temp(5 * i + 1, 0);//将新概率置为0
for(int j = 0; j < dp.size(); j++){
for(int k = 0; k < 6; k++){
//因为每个骰子都等概率投到各个数概率相同
//换言之每个原来的骰子数得到新的骰子数的概率都为1/6
//所以每个新结果都是原来骰子数概率的1/6的累加和
temp[j + k] += dp[j] / 6.0;
}
}
dp = temp;
}
return dp;
}
};