forked from levelp/java_01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecomposition.java
43 lines (38 loc) · 952 Bytes
/
Decomposition.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
/**
* Разложение на слагаемые
*/
public class Decomposition {
int N;
int[] A;
public Decomposition(int N) {
this.N = N;
A = new int[N];
}
/**
* Сгенерировать и напечатать разложение
*/
public void gen() {
rec(0, N, N);
}
private void rec(int index, int max, int sum) {
// Разложение получено
if (sum == 0) {
print(A, index);
return;
}
for (int x = Math.min(max, sum); x >= 1; x--) {
A[index] = x;
rec(index + 1, x, sum - x);
}
}
/**
* @param A массив слагаемых
*/
void print(int[] A, int size) {
System.out.print(N + " = ");
for (int i = 0; i < size - 1; i++) {
System.out.print(A[i] + " + ");
}
System.out.println(A[size - 1]);
}
}