forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test43.java
138 lines (116 loc) · 3.62 KB
/
Test43.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Author: 王俊超
* Date: 2015-06-14
* Time: 12:01
* Declaration: All Rights Reserved !!!
*/
public class Test43 {
/**
* 基于通归求解
*
* @param number 色子个数
* @param max 色子的最大值
*/
public static void printProbability(int number, int max) {
if (number < 1 || max < 1) {
return;
}
int maxSum = number * max;
int[] probabilities = new int[maxSum - number + 1];
probability(number, probabilities, max);
double total = 1;
for (int i = 0; i < number; i++) {
total *= max;
}
for (int i = number; i <= maxSum; i++) {
double ratio = probabilities[i - number] / total;
System.out.printf("%-8.4f", ratio);
}
System.out.println();
}
/**
* @param number 色子个数
* @param probabilities 不同色子数出现次数的计数数组
* @param max 色子的最大值
*/
private static void probability(int number, int[] probabilities, int max) {
for (int i = 1; i <= max; i++) {
probability(number, number, i, probabilities, max);
}
}
/**
* @param original 总的色子数
* @param current 当前处理的是第几个
* @param sum 已经前面的色子数和
* @param probabilities 不同色子数出现次数的计数数组
* @param max 色子的最大值
*/
private static void probability(int original, int current, int sum, int[] probabilities, int max) {
if (current == 1) {
probabilities[sum - original]++;
} else {
for (int i = 1; i <= max; i++) {
probability(original, current - 1, i + sum, probabilities, max);
}
}
}
/**
* 基于循环求解
* @param number 色子个数
* @param max 色子的最大值
*/
public static void printProbability2(int number, int max) {
if (number < 1 || max < 1) {
return;
}
int[][] probabilities = new int[2][max * number + 1];
// 数据初始化
for (int i = 0; i < max * number + 1; i++) {
probabilities[0][i] = 0;
probabilities[1][i] = 0;
}
// 标记当前要使用的是第0个数组还是第1个数组
int flag = 0;
// 抛出一个骰子时出现的各种情况
for (int i = 1; i <= max; i++) {
probabilities[flag][i] = 1;
}
// 抛出其它骰子
for (int k = 2; k <= number; k++) {
// 如果抛出了k个骰子,那么和为[0, k-1]的出现次数为0
for (int i = 0; i < k; i++) {
probabilities[1 - flag][i] = 0;
}
// 抛出k个骰子,所有和的可能
for (int i = k; i <= max * k; i++) {
probabilities[1 - flag][i] = 0;
// 每个骰子的出现的所有可能的点数
for (int j = 1; j <= i && j <= max; j++) {
// 统计出和为i的点数出现的次数
probabilities[1 - flag][i] += probabilities[flag][i - j];
}
}
flag = 1 - flag;
}
double total = 1;
for (int i = 0; i < number; i++) {
total *= max;
}
int maxSum = number * max;
for (int i = number; i <= maxSum; i++) {
double ratio = probabilities[flag][i] / total;
System.out.printf("%-8.4f", ratio);
}
System.out.println();
}
public static void main(String[] args) {
test01();
test02();
}
private static void test01() {
printProbability(2, 4);
}
private static void test02() {
printProbability2(2, 4);
}
}