背包问题(Knapsack problem)的描述为:给定一组物品,每种物品都有自己的重量和价格,在限定的总重量内,我们如何选择,才能使得物品的总价格最高
class Item { public int weigth; public int price; } static int KProblem(int MaxCapacity, Item[] items) { int maxValue = 0; int n = items.Length; int space = 0; //剩余空间 for (int i = 0; i < n; i++) { space = MaxCapacity - items[i].weigth; if (space > 0) { int t = KProblem(space, items) + items[i].price; if (t > maxValue) { maxValue = t; } } } return maxValue; }