Knapsack problem/Continuous: Difference between revisions
→{{header|Pascal}}: simplified version
(added Pascal example) |
(→{{header|Pascal}}: simplified version) |
||
Line 2,620:
constructor Make(const n: string; w, v: Double);
end;
constructor TItem.Make(const n: string; w, v: Double);
Line 2,640 ⟶ 2,633:
Result := CompareValue(R.Price, L.Price);
end;
I: Integer = 0;▼
TArrayHelper<TItem>.Sort(aItems, TComparer<TItem>.Construct(ItemCmp));▼
repeat▼
Result[I].Weight := Min(aItems[I].Weight, aMaxWeight);▼
aMaxWeight := aMaxWeight - Result[I].Weight;▼
Inc(I);▼
until (aMaxWeight <= 0)or(I = Length(aItems));▼
var
Items: array of TItem;
begin
Items := [
Line 2,676 ⟶ 2,650:
TItem.Make('sausage', 5.9, 98)
];
MaxWeight := 15.0;
WriteLn(I.Name, #9, FormatFloat('0.0 kg', I.Weight));▼
I := 0;
▲ repeat
▲ WriteLn(Items[I].Name, #9, FormatFloat('0.0 kg', Items[I].Weight));
▲ Inc(I);
end.
</syntaxhighlight>
|