-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDay31.java
49 lines (40 loc) · 997 Bytes
/
Day31.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.offer;
/**
* n个骰子的点数 打印n个筛子的点数的和,出现的概率
* @author kexun
*
*/
public class Day31 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Day31 d = new Day31();
d.printProbability(5);
}
public void printProbability(int num) {
int[] array = new int[5*num+1];
printProbability(num, array, 0, 6*num);
print(array, num);
}
// 这里用递归调用的方式 但是时间效率有点低
public void printProbability(int num, int[] array, int sum, int max) {
if (num == 1) {
for (int i = 1; i <= 6; i++) {
int value = sum + i;
array[max-value]++;
}
} else {
for (int i = 1; i <= 6; i++) {
int value = sum + i;
printProbability(num-1, array, value, max);
}
}
}
public void print(int[] array, int num) {
int max = 6*num;
for (int i=0; i<array.length; i++) {
if (array[i] != 0) {
System.out.println(max-i + "--" + array[i]);
}
}
}
}