Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for (int i = 1; i < items.Length; i++) // * for each item (ex: beer, wiskey, vodka...)
- {
- for (int k = 1; k < capacity + 1; k++) // * for each capacity of the bag (ex: if max capacity = 5 -> 1,2,3,4,5 / adds 1)
- {
- if (items[i].Weight <= k) // * the weigth of the current item (items[i-1]) is le(less than or equal) to the current bag capacity
- {
- int remainingSpace = k - items[i].Weight; // * remaining space - the difference between the weigth of the currentItem and the current capacity of the bag.
- int valueAbove = valuesArray[i - 1, k]; // * row above, same coll
- if (remainingSpace >= 0)
- {
- int sumValue = items[i].Value + valuesArray[i - 1, remainingSpace]; //* currentItem.Value + value from above row, remainingSpace col.
- if (valueAbove > sumValue) // * gets the biggest value
- {
- valuesArray[i, k] = valueAbove;
- keepArray[i, k] = 0;
- }
- else
- {
- valuesArray[i, k] = sumValue;
- keepArray[i, k] = 1;
- }
- }
- else
- {
- valuesArray[i, k] = valueAbove;
- keepArray[i, k] = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment