-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sword43.java
116 lines (92 loc) · 3.06 KB
/
Sword43.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
package offer.fifty;
/**
* Created by colin on 2017/5/14.
*/
public class Sword43 {
/*
n个骰子的点数,这道题没啥可写的
*/
public static void main(String[] args) {
}
/*
* 基于递归求解
*/
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();
}
private static void probability(int number, int[] probabilities, int max) {
for (int i = 1; i <= max; i++) {
probability(number, number, i, probabilities, 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);
}
}
}
/*
* 基于循环求解
*/
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();
}
}