Knapsack problem/Unbounded: Difference between revisions

(Contribution reconstruction due to vandalism.)
Line 740:
</pre>
 
=={{header|C#}}==
<lang csharp>/* Items Value Weight Volume
a 30 3 25
b 18 2 15
c 25 20 2
 
<=250 <=250 */
using System;
class Program
{
static void Main()
{
uint[] r = items1();
Console.WriteLine(r[0] + " v " + r[1] + " a " + r[2] + " b"); // 0 15 11
var sw = System.Diagnostics.Stopwatch.StartNew();
for (int i = 1000; i > 0; i--) items1();
Console.Write(sw.Elapsed); Console.Read();
}
 
static uint[] items0() // 1.2 µs
{
uint v, v0 = 0, a, b, c, a0 = 0, b0 = 0, c0 = 0;
for (a = 0; a <= 10; a++)
for (b = 0; a * 5 + b * 3 <= 50; b++)
for (c = 0; a * 25 + b * 15 + c * 2 <= 250 && a * 3 + b * 2 + c * 20 <= 250; c++)
if (v0 < (v = a * 30 + b * 18 + c * 25))
{
v0 = v; a0 = a; b0 = b; c0 = c;
//Console.WriteLine("{0,5} {1,5} {2,5} {3,5}", v, a, b, c);
}
return new uint[] { a0, b0, c0 };
}
 
static uint[] items1() // 0,22 µs
{
uint v, v0 = 0, a, b, c, a0 = 0, b0 = 0, c0 = 0, c1 = 0;
for (a = 0; a <= 10; a++)
for (b = 0; a * 5 + b * 3 <= 50; b++)
{
c = (250 - a * 25 - b * 15) / 2;
if ((c1 = (250 - a * 3 - b * 2) / 20) < c) c = c1;
if (v0 < (v = a * 30 + b * 18 + c * 25))
{ v0 = v; a0 = a; b0 = b; c0 = c; }
}
return new uint[] { a0, b0, c0 };
}
}</lang>
=={{header|Clojure}}==
<lang lisp>(defstruct item :value :weight :volume)
Anonymous user