LintCode 20. Dices Sum

 
    vector<pair<int, double>> dicesSum(int n) {
        // Write your code here
       
            vector<vector<double>> dp;
            dp.resize(n+1);
            int cnt = 1;
            for(auto& r: dp){
                r.resize(cnt*6+1);
                cnt++;
            }
            for(int i=1;i<=6;++i){
                dp[1][i] = 1.0/6;
            }
            for(int i=2;i<=n;++i){
                for(int j=i;j<=i*6;++j){
                    for(int k = 1;k<=6;++k){
                        if((j-k) >= (i-1) && (j-k) <= (i-1)*6){
                            dp[i][j]+=dp[i-1][j-k];
                        }
                    }
                    dp[i][j]/=6.0;
                }
            }
            vector<pair<int, double>> ans;
            ans.reserve(n*6-n+1);
            for(int point=n;point<=n*6;++point){
                ans.push_back(make_pair(point, dp[n][point]));
            }
            return ans;
    }
};
posted @ 2021-03-27 19:11  rxh1999  阅读(33)  评论(0编辑  收藏  举报