My goal is to provide the program with several elements (rows), range and target percentage, and let this give me all possible percentages of each element. For example, imagine that you go to a grocery store and have a basket of apples and pears that you want to know all the percentages that you could use using ALL items (not a complete solution, I do it manually):
{Apple:50, Pears:50}, {Apple:75, Pears:25}, {Apple:90, Pears:10},etc.
If I do the same with a range of 20-50 (which means that the largest value for one element can be 50% and the lowest 20%), then the only result:
{Apple:50, Pears:50}(since there are only 2 items, and it can not exceed 50 % weight)
I thought it had similarities, like a knapsack problem, with a few big differences, since there are no values / weights associated with the elements (but, like a knapsack problem, trying to put elements in an Im backpack, trying to match the values in target_percent, 100 %). I am also having problems applying the general ideas of dynamic programming, since I cannot figure out how to fix the problem (typical problems with rucksacks produce results, and then "cache results for reuse", but if I have a list of X elements, I need all X elements to be used within the range).
I can do this with brute force, but I don’t feel that it is effective, because it just tries everything so that the boundaries that I use arent used to make it effective at all (for example, if the apple is 75 %, then theres no reason Pear should exceed 25%. Limitations are the size of the list, range and target_percent. I can have 20-30 list elements with a range of 5-20 or maybe 50 elements with a range of 1 to 5 or any other I want to play around with how many complete results I can get as quickly as possible.I did not show the target_percent part in the question, because I can configure it as soon as I understand how to solve the problem, but basically all the examples assume 100% max, but sometimes you may already have 20% oranges in the basket and see how you can use Apples / Pears,to replenish the remaining 80%).
: ( , -, )? , ? (, , )? - , , ( 2 , , , ). ( , , , , , )?
() ( , , , , ):
import java.util.ArrayList;
import java.util.Arrays;
public class brute_force_percent_returner {
static String[] data = new String[]{"Apple", "Pears"};
static int[] coeff = new int[data.length];
static ArrayList<int[]> queue = new ArrayList<int[]>();
public static void main(String[] args) {
System.out.println("Starting");
recursion(0,data);
for (int[] item : queue) {
for (int item2 = 0; item2<data.length; item2++) {
System.out.print(data[item2] + " = " + item[item2] + " ");
}
System.out.println();
}
}
private static void recursion(int k, String[] data2) {
for (String item: data2) {
for (int x = 0; x<5;x++) {
int[] coeff_temp = Arrays.copyOf(coeff, coeff.length);
coeff_temp[k] = x;
queue.add(coeff_temp);
}
}
if (k == data.length-1) {
return;
} else {
recursion(k+1, data2);
}
}
}
, , ( , , , , , , , - ):
public class TurboAdder {
private static final int[] data = new int[] { 5, 10, 20, 25, 40, 50 };
private static class Node {
public final int index;
public final int count;
public final Node prevInList;
public final int prevSum;
public Node(int index, int count, Node prevInList, int prevSum) {
this.index = index;
this.count = count;
this.prevInList = prevInList;
this.prevSum = prevSum;
}
}
private static int target = 100;
private static Node sums[] = new Node[target+1];
private static boolean forbiddenValues[] = new boolean[data.length];
public static void printString(String prev, Node n) {
if (n == null) {
System.out.println(prev);
} else {
while (n != null) {
int idx = n.index;
if (!forbiddenValues[idx]) {
forbiddenValues[idx] = true;
printString((prev == null ? "" : (prev+" + "))+data[idx]+"*"+n.count, sums[n.prevSum]);
forbiddenValues[idx] = false;
}
n = n.prevInList;
}
}
}
public static void main(String[] args) {
for (int i = 0; i < data.length; i++) {
int value = data[i];
for (int count = 1, sum = value; count <= 100 && sum <= target; count++, sum += value) {
for (int newsum = sum+1; newsum <= target; newsum++) {
if (sums[newsum - sum] != null) {
sums[newsum] = new Node(i, count, sums[newsum], newsum - sum);
}
}
}
for (int count = 1, sum = value; count <= 100 && sum <= target; count++, sum += value) {
sums[sum] = new Node(i, count, sums[sum], 0);
}
}
printString(null, sums[target]);
}
}