Knapsack problem/Continuous: Difference between revisions
Content added Content deleted
(added Pascal example) |
(→{{header|Pascal}}: simplified version) |
||
Line 2,620: | Line 2,620: | ||
constructor Make(const n: string; w, v: Double); |
constructor Make(const n: string; w, v: Double); |
||
end; |
end; |
||
TSelItem = record |
|||
Name: string; |
|||
Weight: Double; |
|||
end; |
|||
THaul = array of TSelItem; |
|||
constructor TItem.Make(const n: string; w, v: Double); |
constructor TItem.Make(const n: string; w, v: Double); |
||
Line 2,640: | Line 2,633: | ||
Result := CompareValue(R.Price, L.Price); |
Result := CompareValue(R.Price, L.Price); |
||
end; |
end; |
||
function Solve(var aItems: array of TItem; aMaxWeight: Double): THaul; |
|||
var |
|||
⚫ | |||
begin |
|||
if Length(aItems) = 0 then exit(nil); |
|||
⚫ | |||
SetLength(Result, Length(aItems)); |
|||
⚫ | |||
Result[I].Name := aItems[I].Name; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
SetLength(Result, I); |
|||
end; |
|||
const |
|||
MAX_WEIGHT = Double(15); |
|||
var |
var |
||
Items: array of TItem; |
Items: array of TItem; |
||
MaxWeight: Double; |
|||
⚫ | |||
begin |
begin |
||
Items := [ |
Items := [ |
||
Line 2,676: | Line 2,650: | ||
TItem.Make('sausage', 5.9, 98) |
TItem.Make('sausage', 5.9, 98) |
||
]; |
]; |
||
⚫ | |||
for I in Solve(Items, MAX_WEIGHT) do |
|||
MaxWeight := 15.0; |
|||
⚫ | |||
I := 0; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end. |
end. |
||
</syntaxhighlight> |
</syntaxhighlight> |