//这个是简单优化过的,开始我写的以为顺序有关系,慢太多了
procedure calc1(const srcLong, srcHieght: array of Double; var Result: array of Double);
var
i, k: Integer;
l, h: Integer;
flag: Boolean;
temp:Double;
begin
l := Low(srcLong);
h := High(srclong);
//获得有效体积(比例),宽,高一样,不用考虑
for i := h downto l do
begin
Result[i] := srcHieght[i] * srcLong[i];
end;
while True do
begin
Application.ProcessMessages;
flag := True;
for i := h downto l + 1 do
begin
//if ((Result[i] / srcLong[i]) > (Result[i - 1] / srclong[i - 1])) then
if ((Result[i] / srcLong[i]) - (Result[i - 1] / srclong[i - 1]) > 0.0001) then
begin
temp:= (result[i]+ result[i-1])/ (srcLong[i]+srcLong[i-1]);
result[i] := temp*srcLong[i];
result[i - 1] := temp*srcLong[i-1];
flag := False;
end;
end;
//满足结算出实际高度
if flag then
begin
for k := h downto l do
begin
Result[k] := Result[k] / srcLong[k];
end;
Exit;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const MAXLONG=25;
var
_srcLong, _srcHieght: array of Double;
_Result: array of Double;
i:Integer;
begin
SetLength(_srcLong, MAXLONG);
SetLength(_srcHieght, MAXLONG);
SetLength(_Result, MAXLONG);
Randomize;
for i := 0 to MAXLONG-1 do
begin
_srcLong[i]:=RandomRange(10,2000);
_srcHieght[i]:=RandomRange(100,2000);
end;
Memo2.Lines.Clear;
calc1(_srcLong, _srcHieght, _Result);
Memo2.Lines.Add('Height -- Length:');
for i := 0 to MAXLONG-1 do
begin
Memo2.Lines.Add(FormatFloat('0.#',_srcHieght[i])+ ' -- ' + FormatFloat('0.#',_srcLong[i]));
end;
Memo2.Lines.Add('RESULT:') ;
for i := 0 to MAXLONG-1 do
begin
Memo2.Lines.Add(FormatFloat('0.0###', RoundTo(_Result[i],-4)));
end;
end;