Hello,
for my study purpose I need to build an array of array filled with the partitions of an integer with fixed term. That is given an integer, suppose 10 and given the fixed number of terms, suppose 5 I need to populate an array like this
10 0 0 0 0
9 0 0 0 1
8 0 0 0 2
7 0 0 0 3
............
9 0 0 1 0
8 0 0 1 1
.............
7 0 1 1 0
6 0 1 1 1
............
...........
0 6 1 1 1
.............
0 0 0 0 10
am pretty new to Java and am getting confused with all the for loops. Right now my code can do the partition of the integer but unfortunately it is not with fixed term
public class Partition {
private static int[] riga;
private static void printPartition(int[] p, int n) {
for (int i= 0; i < n; i++)
System.out.print(p[i]+" ");
System.out.println();
}
private static void partition(int[] p, int n, int m, int i) {
if (n == 0)
printPartition(p, i);
else
for (int k= m; k > 0; k--) {
p[i]= k;
partition(p, n-k, n-k, i+1);
}
}
public static void main(String[] args) {
riga = new int[6];
for(int i = 0; i<riga.length; i++){
riga[i] = 0;
}
partition(riga, 6, 1, 0);
}
}
the output I get it from is like this:
1 5
1 4 1
1 3 2
1 3 1 1
1 2 3
1 2 2 1
1 2 1 2
1 2 1 1 1
what i'm actually trying to understand how to proceed is to have it with a fixed terms which would be the columns of my array. So, am stuck with trying to get a way to make it less dynamic. Any help?