Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Knapsack Problem
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { // Knapsack Problem public static void Main(string[] args) { int[] value = { 10, 50, 70 }; int[] weight = { 10, 20, 30 }; int capacity = 40; int itemsCount = 3; int result = KnapSack(capacity, weight, value, itemsCount); Console.WriteLine(result); } public static int KnapSack(int capacity, int[] weight, int[] value, int itemsCount) { int[,] K = new int[itemsCount + 1, capacity + 1]; for (int i = 0; i <= itemsCount; ++i) { for (int w = 0; w <= capacity; ++w) { if (i == 0 || w == 0) K[i, w] = 0; else if (weight[i - 1] <= w) K[i, w] = Math.Max(value[i - 1] + K[i - 1, w - weight[i - 1]], K[i - 1, w]); else K[i, w] = K[i - 1, w]; } } return K[itemsCount, capacity]; } } }
run
|
edit
|
history
|
help
0
Nice Stuff
Linq
Square Integer Matrix
merge sort
a
6
Program
C
Delegate-ModifyObject
Fórum ➡ Finding STRONG tags that contain UL tags, using Regex class♦