[LintCode]Dices Sum
来源:互联网 发布:小额网络个人基金产品 编辑:程序博客网 时间:2024/06/05 20:22
http://www.lintcode.com/en/problem/dices-sum/
骰子投掷n次,求所有情况的和出现的概率
二维数组dp[i][j]保存投掷i次得到和为j的概率,当前位置的概率为当前投掷1 ~ 6的情况下的前序概率和
public class Solution { /** * @param n an integer * @return a list of Map.Entry<sum, probability> */ public List<Map.Entry<Integer, Double>> dicesSum(int n) { // Write your code here // Ps. new AbstractMap.SimpleEntry<Integer, Double>(sum, pro) // to create the pair List<Map.Entry<Integer, Double>> res = new LinkedList(); double[][] dp = new double[n + 1][6 * n + 1]; for (int i = 1; i <= 6; i++) { dp[1][i] = 1.0 / 6.0; } for (int i = 2; i <= n; i++) { for (int j = i; j <= 6 * i; j++) { for (int k = 1; k <= 6; k++) { if (j > k) { dp[i][j] += dp[i - 1][j - k]; } } dp[i][j] /= 6.0; } } for (int i = n; i <= 6 * n; i++) { res.add(new AbstractMap.SimpleEntry<Integer, Double>(i, dp[n][i])); } return res; }}
0 0
- [LintCode]Dices Sum
- lintcode: Dices Sum
- #20 Dices Sum
- Lintcode20 Dices Sum solution 题解
- Lintcode - k sum II
- [LintCode]k Sum
- [LintCode]k Sum II
- 2 Sum-LintCode
- lintcode:Continuous Subarray Sum
- lintcode Interval Sum
- lintcode:3 Sum Closest
- lintcode:3 Sum
- lintcode:2sum
- lintcode:Subarray Sum Closest
- [LintCode]Submatrix Sum
- [LintCode] Continuous Subarray Sum
- [Lintcode]Minimum Path Sum
- lintcode: Subarray Sum Closest
- 关于几个主流语音SDK的接入问题
- 动态规划之图像压缩算法
- nyoj-过河问题
- C++中动态存储分配的基础知识
- java 的JVM内存详解和内存溢出异常
- [LintCode]Dices Sum
- 【Sikuli】Sikuli安装过程
- OpenGL渲染流程
- 基于Slim微型框架实现强大的API—— Slim入门篇
- 【JZOJ 3749】【Srm590】Fox And City
- poj 3244 公式推导 叉姐的魔法训练
- iOS 把String字符串转化为json字符串-subString截取字符串-index操作
- nswoj-zb的生日
- Java I/O流(二)——File,Properties,PrintWriter,IO ,RandomAccessFile,ByteBuffer,nio