My goal is to give the program a few items(Strings), a range, and target percent and let it give me all possible percentages of each item. For example, Imagine you go to the grocery store and have a basket of Apples & Pears you want to know all the percentages you could have using ALL items(not a full solution, I m doing this by hand):
{Apple:50, Pears:50}, {Apple:75, Pears:25}, {Apple:90, Pears:10},etc.
If I do the same thing with a range of 20-50(meaning the highest value a single item can have is 50% and the lowest 20%) then the only result is:
{Apple:50, Pears:50}
(since there are only 2 items and it cannot exceed 50% weight)
我认为它也有类似的特质,比如“背包问题 ” ( knapsack problem)与“背包问题 ” ( knapsack problem)没有关联到这些物品的价值/重量(但就像“背包问题 ” ( knapsack problem ), 试图将物品与“背包” 匹配到“背包项目 ” ( knapsack problem ) 。 我试图将价值与“目标 ” ( 百分比, 100% ) 。 我也很难应用一般动态的编程想法, 也因为我无法找出如何打破问题( 典型的背包问题正在形成结果, ” 和“缓冲” 的结果再利用, 但如果我有一个 X 项清单, 我需要所有X 物品在范围内使用 ) 。
我可以通过粗力做到这一点, 但我并不觉得它是有效的, 因为它只是试探一切我正在使用的界限, 根本用不着用它来提高效率(例如,苹果为75%,那么Pear就没有任何理由超过25%)。. 范围是列表、范围和目标的百分之二十五的大小。 我可能有20-30个清单项目,范围在5-20或大约50个项目之间,范围在1-5之间。 或任何我之间想要玩弄的,我可以得到的尽可能快的多少完整结果。 我还没有在问题中显示目标-%的部分,因为我可以设置它,一旦我了解如何解决问题,但基本上所有的例子都假定是百分之百,但有时你的篮子里已经有20%的橘子,看看如何用苹果/梨来填满其余的百分之八十。
我的问题是,我怎样才能解决这个问题(使用任何想法的逻辑、例子或代理问题)? 动态编程是否适合这一问题? 动态编程是否适合这一问题,或者我无法将它分成小块的问题(因为它总是包括列表中的所有项目,所以没有建立起来 )? 如果有人能指向我正确的方向,我愿意研究任何可能有所帮助的议题(在花了2天时间试图弄清楚这一点之后,我不确定动态编程路线是否正确 ) 。 是否还有这类问题的名称(我查了Knapack问题、整形分割问题、组合法问题,但没有人认为合适 )?
我的野蛮武力方法(其实不像预期的那样有效, 但也许能让你们了解野蛮武力方法):
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) {
// this is not exactly working
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];
// Only for use by printString.
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;
// We prevent recursion on a value already seen.
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]);
}
}