Solved
An algorithm to deposit boxes
Posted on 2000-01-09
I am not sure it is a question for DELPHI.
There are three boxes:
BoxA(Height=nAHeight,NUM=nANum),
BoxB(Height=nBHeight,NUM=nBNum),
BoxC(Height=nCHeight,NUM=nCNum),
now pick at least each of them to deposit to a fixed height(nHeightWanted), and make the error smallest.
I wrote the following code,is there a better way?
procedure procPick;
var
I,J,K : Integer;
rMin,rTemp : Real;
IJKList : TStrings;
begin
IJKList : TStringList.Create;
rMin := nAHeight;
for I := 1 to nANum
for J := 1 to nBNum
for K := 1 to nCNum
begin
rTemp := nHeightWanted - nAHeight*I - nBHeight*J - nCHeight*K;
if(rTemp < 0) then Break;
if(rTemp <= rMin) then
begin
if(rTemp < rMin) then
IJKList.Clear;
IJKList.Add(I);
IJKList.Add(J);
IJKList.Add(K);
end;
end;
{something else}
IJKList.Free;
end;