骰子求和

1、题目描述

要求:扔 n 个骰子,向上面的数字之和为 S。给定 Given n,请列出所有可能的 S 值及其相应的概率。
样例:
输入

1

输出:

[ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, 0.17], [6, 0.17]]

2、解决代码

(1)解题思路

1个骰子,S的取值区间为1~6;
2个骰子,S的取值区间为2~12;
3个骰子,S的取值区间为3~18...

因此可以创建一个二维数组a[n][n*6],该数组的每一行代表一种情况:a[1][]代表一个骰子的情况;a[2][]代表两个骰子的情况...
在n-1个骰子的情况下,我们新加上一个骰子,此时和为S的骰子出现的次数应该等于n-1个骰子时和为S-1、S-2、S-3、S-4、S-5与S-6的次数的总和。
n = 1时: f(1,1) = f(1,2) = f(1,3) = f(1,4) = f(1,5) = f(1,6) = 1
而 n = 2时:f(2,2) = f(1,1) = 1
f(2,3) = f(1,2) + f(1,1) = 2
...
f(2,6) = f(1,5) + f(1,4) + f(1,3) + f(1,2) + f(1,1)

f(2,7) = f(1,6) + f(1,5) + f(1,4) + f(1,3) + f(1,2) + f(1,1) = 6

(2)基于以上思想,可以写出以下的代码

import java.text.DecimalFormat;
import java.util.*;

public class Main3 {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {//注意while处理多个case
            int n = in.nextInt();
            List<Map.Entry<Integer, String>> result = dicesSum(n);
            for (int i = 0; i < result.size(); ++i) {
                Map.Entry<Integer, String> map = result.get(i);
                if (i == 0)
                    System.out.print("[[" + map.getKey() + ", " + map.getValue() + "], ");
                else if (i == result.size() - 1)
                    System.out.println("[" + map.getKey() + ", " + map.getValue() + "]]");
                else {
                    System.out.print("[" + map.getKey() + ", " + map.getValue() + "], ");
                }

            }
        }
    }

    public static List<Map.Entry<Integer, String>> dicesSum(int n) {

        DecimalFormat df = new DecimalFormat("0.00000");
        long[][] dp = new long[n + 1][6 * n + 1];
        dp[1][1] = 1;
        dp[1][2] = 1;
        dp[1][3] = 1;
        dp[1][4] = 1;
        dp[1][5] = 1;
        dp[1][6] = 1;
        //计算2个以上骰子可能出现的和及其对应的次数
        for (int i = 2; i <= n; i++) {
            for (int j = i; j <= i * 6; j++) {
                long x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0;
                if (j - 1 > 0) {
                    x1 = dp[i - 1][j - 1];
                }
                if (j - 2 > 0) {
                    x2 = dp[i - 1][j - 2];
                }
                if (j - 3 > 0) {
                    x3 = dp[i - 1][j - 3];
                }
                if (j - 4 > 0) {
                    x4 = dp[i - 1][j - 4];
                }
                if (j - 5 > 0) {
                    x5 = dp[i - 1][j - 5];
                }
                if (j - 6 > 0) {
                    x6 = dp[i - 1][j - 6];
                }
                dp[i][j] = x1 + x2 + x3 + x4 + x5 + x6;
            }
        }
        List<Map.Entry<Integer, String>> result = new ArrayList<>();
        for (int i = n; i <= 6 * n; i++) {
            AbstractMap.SimpleEntry<Integer, String> entry = new AbstractMap.SimpleEntry<>(i, df.format(dp[n][i] / Math.pow(6, n)));
            result.add(entry);
        }
        return result;
    }
}

参考文章:https://blog.csdn.net/aphysia/article/details/77855488

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 14,084评论 6 13
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,248评论 0 2
  • 作者:龙门派·黄永亮 第十一回降冰雹天公护法施妙算真人指迷 陷溺沉沦己有年,爱河滚滚浪滔天。修行自可登高岸,何用中...
    石竹阅读 4,068评论 0 4
  • 今天的蛋糕,大家都说很美,是为了颜值买的。可是老板给我搞错配色了,我想要的是粉色配白色,因为是少女的感觉。这是今天...
    aseeya阅读 2,563评论 0 0

友情链接更多精彩内容