剑指offer第二版-60.n个骰子的点数

本系列导航:剑指offer(第二版)java实现导航帖

面试题60:n个骰子的点数

题目要求:
把n个骰子仍在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值的出现概率。

解题思路:
新加入一个骰子,它出现1-6的概率是相等的,可以看成各出现一次,那么出现和为s的次数等于再加入之前出现和为s-1,s-2,s-3,s-4,s-5,s-6这6种情况的次数之和。如此循环,直到加入n个骰子结束。

package chapter6;

/**
 * Created with IntelliJ IDEA
 * Author: ryder
 * Date  : 2017/8/20
 * Time  : 12:07
 * Description:n个骰子的点数
 **/
public class P294_DicesProbability {
    public static void printProbability(int number){
        if(number<=0)
            return;
        int result[][] = new int[2][6*number+1];
        for(int i=1;i<=6;i++)
            result[1][i] = 1;
        for (int num=2;num<=number;num++){
            for(int i=num;i<6*num+1;i++){
                for(int j=i-6;j<i;j++)
                    if(j>0)
                        result[num%2][i] += result[(num-1)%2][j];
            }
        }
        double sum = 0;
        for(int i=number;i<6*number+1;i++)
            sum += result[number%2][i];
        System.out.println("number = "+number);
        for(int i=number;i<6*number+1;i++)
            System.out.println("probability "+i+":"+result[number%2][i]/sum);
    }
    public static void main(String[] args){
        printProbability(2);
        printProbability(0);
        printProbability(11);
    }
}

运行结果

number = 2
probability 2:0.027777777777777776
probability 3:0.05555555555555555
probability 4:0.08333333333333333
probability 5:0.1111111111111111
probability 6:0.1388888888888889
probability 7:0.16666666666666666
probability 8:0.1388888888888889
probability 9:0.1111111111111111
probability 10:0.08333333333333333
probability 11:0.05555555555555555
probability 12:0.027777777777777776
number = 11
probability 11:1.6756188667183114E-5
probability 12:3.579187959242291E-5
probability 13:7.032992429171074E-5
probability 14:1.305226912585078E-4
probability 15:2.3024059318411616E-4
probability 16:3.9040115984551727E-4
probability 17:6.357051559398802E-4
probability 18:0.001001877097313203
probability 19:0.0015325860758414697
probability 20:0.002280110704798586
probability 21:0.003305202218339453
probability 22:0.004674033545694139
probability 23:0.00645553518364798
probability 24:0.008715293344929922
probability 25:0.011508426785284112
probability 26:0.01487104681884247
probability 27:0.01881072643451761
probability 28:0.023297934235086463
probability 29:0.028258344104977472
probability 30:0.03356749826313445
probability 31:0.039050320505623064
probability 32:0.044485514406365174
probability 33:0.04961570916656004
probability 34:0.05416335311279245
probability 35:0.05785216796375705
probability 36:0.060432555827780905
probability 37:0.0617072952125485
probability 38:0.06155506183772446
probability 39:0.0599479627662407
probability 40:0.05695967220644532
probability 41:0.05276191338611994
probability 42:0.047609030081524346
probability 43:0.04181271652039078
probability 44:0.035709917470240465
probability 45:0.029628809546438983
probability 46:0.023858031191248626
probability 47:0.018623457392480367
probability 48:0.014075165885072643
probability 49:0.010285264127013152
probability 50:0.007255573112479451
probability 51:0.004932366994161532
probability 52:0.0032246655999523255
probability 53:0.002022702964588517
probability 54:0.0012138990566254915
probability 55:6.946788563987938E-4
probability 56:3.775520684709804E-4
probability 57:1.9390328454805907E-4
probability 58:9.350957213412316E-5
probability 59:4.199803635982986E-5
probability 60:1.7377673553555097E-5
probability 61:6.525591306905834E-6
probability 62:2.1751971023019446E-6
probability 63:6.214848863719843E-7
probability 64:1.434195891627656E-7
probability 65:2.390326486046093E-8
probability 66:2.173024078223721E-9

推荐阅读更多精彩内容

  • 你的数学直觉怎么样?你能凭借直觉,迅速地判断出谁的概率大,谁的概率小吗?下面就是 26 个这样的问题。如果你感兴趣...
    cnnjzc阅读 3,057评论 0 11
  • 题目:把n个骰子仍在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。 n个骰子的点...
    qmss阅读 919评论 0 0
  • 忘光了概率统计的知识还想学朴素贝叶斯算法?这一篇就是为你准备的。虽然如此,作为初学者,别指望 5 分钟就能完全理解...
    kamidox阅读 1,688评论 3 5
  • 嗨,简书的未曾谋面的你们,从今天,患病的第16天,我想开始和你说悄悄话了
    晓晓的奇迹之旅阅读 27评论 0 0
  • 友盟第三方登录和友盟分享是在一起的,首先注册友盟账号,生成APPKey,这步省略。。。。。。。。。。 1.下载需要...
    Ayres阅读 164评论 0 2