Knapsack problem/0-1: Difference between revisions
Content added Content deleted
(Knapsack problem/0-1 in FreeBASIC) |
|||
Line 2,820: | Line 2,820: | ||
Weight: 396 Value: 1030 |
Weight: 396 Value: 1030 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|XPL0}} |
|||
<lang freebasic>#define Tabu = Chr(9) |
|||
Dim As Integer i, A, P, V, N |
|||
Dim As Integer MejorArticulo, MejorValor = 0 |
|||
Type Knapsack |
|||
articulo As String*22 |
|||
peso As Integer |
|||
valor As Integer |
|||
End Type |
|||
Dim item(1 To 22) As Knapsack => { _ |
|||
("map ", 9, 150), ("compass ", 13, 35), _ |
|||
("water ", 153, 200), ("sandwich ", 50, 160), _ |
|||
("glucose ", 15, 60), ("tin ", 68, 45), _ |
|||
("banana ", 27, 60), ("apple ", 39, 40), _ |
|||
("cheese ", 23, 30), ("beer ", 52, 10), _ |
|||
("suntan cream ", 11, 70), ("camera ", 32, 30), _ |
|||
("T-shirt ", 24, 15), ("trousers ", 48, 10), _ |
|||
("umbrella ", 73, 40), ("waterproof trousers ", 42, 70), _ |
|||
("waterproof overclothes", 43, 75), ("note-case ", 22, 80), _ |
|||
("sunglasses ", 7, 20), ("towel ", 18, 12), _ |
|||
("socks ", 4, 50), ("book ", 30, 10)} |
|||
For i = 1 To (1 Shl 22)-1 |
|||
A = i : P = 0 : V = 0 : N = 1 |
|||
While A |
|||
If A And 1 Then |
|||
P += item(N).peso |
|||
V += item(N).valor |
|||
End If |
|||
A Shr= 1 |
|||
N += 1 |
|||
Wend |
|||
If V > MejorValor And P <= 400 Then |
|||
MejorValor = V |
|||
MejorArticulo = i |
|||
End If |
|||
Next |
|||
A = MejorArticulo : P = 0 : V = 0 : N = 1 |
|||
While A |
|||
If A And 1 Then |
|||
Print " "; item(N).articulo; Tabu; |
|||
Print item(N).peso; Tabu; item(N).valor |
|||
P += item(N).peso |
|||
V += item(N).valor |
|||
End If |
|||
A Shr= 1 : N += 1 |
|||
Wend |
|||
Print "Totals:"; Spc(25); P; Tabu; V |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre>Same as XLP0 entry.</pre> |
|||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |