Untitled
unknown
java
4 years ago
1.5 kB
4
Indexable
public class MyClass {
public static void main(String args[]) {
int[] weights = {1};
int[] values = {3};
knapsack(weights, values, 2);
}
static void knapsack(int[] weights, int[] values, int bound) {
System.out.println("1");
if(weights.length != values.length || bound <= 0) {
System.out.println("2 - end");
return;
}
System.out.println("3");
int objects = weights.length;
int[][] r = new int[objects + 1][bound + 1];
for(int i = objects - 1; i>=0; i--) {
System.out.println("4");
for(int j = 1; j <= bound; j++) {
System.out.println("5");
System.out.println("6");
if(weights[i] <= j) {
System.out.println("7");
int valWithI = values[i] + r[i+1][j-weights[i]];
int valWithoutI = r[i+1][j];
System.out.println("8");
if(valWithI > valWithoutI) {
System.out.println("9");
r[i][j] = valWithI;
} else {
System.out.println("10");
r[i][j] = valWithoutI;
}
} else {
System.out.println("11");
r[i][j] = r[i+1][j];
}
}
}
System.out.println("12 - e");
return;
}
}Editor is loading...