Dice Game题号：205 难度： 15 中英对照

Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.

Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.

What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg

Code

public class p205 {

public static void main(String[] args) {
long start = System.nanoTime();
double result = run();
long end = System.nanoTime();
System.out.println(result);
System.out.println((end - start) / 1000000 + "ms");
}

private  static int MAX = 37; // 骰子最大掷出点数为36

private static double run() {

int[] peterCount = new int[MAX];
int[] colinCount = new int[MAX];

//get peter's possible totals for all possible rolls of the dice
for (int d1 = 1; d1 <= 4; d1++)
for (int d2 = 1; d2 <= 4; d2++)
for (int d3 = 1; d3 <= 4; d3++)
for (int d4 = 1; d4 <= 4; d4++)
for (int d5 = 1; d5 <= 4; d5++)
for (int d6 = 1; d6 <= 4; d6++)
for (int d7 = 1; d7 <= 4; d7++)
for (int d8 = 1; d8 <= 4; d8++)
for (int d9 = 1; d9 <= 4; d9++) {
peterCount[d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9]++;
}

//get colin's possible totals for all possible rolls of the dice
for (int d1 = 1; d1 <= 6; d1++)
for (int d2 = 1; d2 <= 6; d2++)
for (int d3 = 1; d3 <= 6; d3++)
for (int d4 = 1; d4 <= 6; d4++)
for (int d5 = 1; d5 <= 6; d5++)
for (int d6 = 1; d6 <= 6; d6++) {
colinCount[d1 + d2 + d3 + d4 + d5 + d6]++;
}

//how many possible throws are there?
int peterTotal = power(4,9);
int colinTotal = power(6,6);

//what are the probabilities of throwing a certain total?
double[] peterProb = new double[MAX];
double[] colinProb = new double[MAX];

for (int i = 0; i < MAX; i++) {
peterProb[i] = (double) peterCount[i] / peterTotal;
colinProb[i] = (double) colinCount[i] / colinTotal;
}

//the probability that peter wins is:
// probability that colin throws a number * prob that peter throws a higher number
double peterWins = 0;
for (int n = 1; n < peterCount.length; n++) {
peterWins += colinProb[n - 1] * arraySum(peterProb, n, peterCount.length);
}

// 保留小数位数
peterWins = Double.parseDouble(String.format("%.7f", peterWins));
return peterWins;
}

private static double arraySum(double[] a, int start, int end) {

double sum = 0;

for (int i = start; i < end; i++)
sum += a[i];
return sum;
}

private static int power(int x,int n){
int prod = 1;
for(int i = 0;i<n;i++){
prod *= x;
}
return  prod;
}

}

0.5731441
22ms