Sword refers to the points of Offer66-n dice

public double[] dicesProbability(int n) {
    // initialize a sieve
    double[] dp = new double[6];
    // The initialization array is all one-sixth
    Arrays.fill(dp, 1.0 / 6.0);
    for (int i = 2; i <= n; i++) {
        // Create a temporary result set
        double[] tmp = new double[5 * i + 1];
        for (int j = 0; j < dp.length; j++) {
            for (int k = 0; k < 6; k++) {
                // Recursion formula
                tmp[j + k] += dp[j] / 6.0;
            }
        }
        // save the result
        dp = tmp;
    }
    return dp;
}

Guess you like

Origin blog.csdn.net/a792396951/article/details/114891788