[C#] C#递归解决背包问题代码 →→→→→进入此内容的聊天室

来自 , 2021-02-07, 写在 C#, 查看 105 次.
URL http://www.code666.cn/view/6b9bb055
  1. class Item {
  2.            public int weigth;
  3.            public int price;
  4.         }
  5.         static int KProblem(int MaxCapacity, Item[] items)
  6.         {
  7.             int maxValue = 0;
  8.             int n = items.Length;
  9.             int space = 0; //剩余空间
  10.             for (int i = 0; i < n; i++)
  11.             {
  12.                 space = MaxCapacity - items[i].weigth;
  13.                 if (space > 0)
  14.                 {
  15.                     int t = KProblem(space, items) + items[i].price;
  16.                     if (t > maxValue)
  17.                     {
  18.                         maxValue = t;
  19.                     }
  20.                 }
  21.             }
  22.             return maxValue;
  23.         }
  24. //csharp/7209

回复 "C#递归解决背包问题代码"

这儿你可以回复上面这条便签

captcha