剑指OFFER----面试题60. n个骰子的点数

链接:https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/

代码

class Solution {
public:
    vector<double> twoSum(int n) {
        vector<double> res(6 * n + 1, 0.0);
        const double p = 1.0 / 6.0;
        for (int i = 1; i <= 6; ++i) res[i] = p;

        for (int i = 2; i <= n; ++i) {
            for (int j = 6 * i; j >= i; --j) {
                res[j] = 0;
                for (int k = j - 1; k >= i - 1 && k >= j - 6; --k) {
                    res[j] += res[k] * p;
                }
            }
        }
        return vector<double>(res.begin() + n, res.end());
    }
};
posted @ 2020-03-13 13:42  景云ⁿ  阅读(94)  评论(0编辑  收藏  举报