谢谢elovem:
这里的这个问题我翻以前的贴子弄出来了,就我们现在这个版块的版主LeeChange写的,代码如下:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
a: array [1..25] ofdo
uble = (9177.95, 15352.74, 73037.94, 42818.49, 66395.85,
76798.8, 4537.96, 13539.47, 43244.64, 13539.47,
109395, 95775.26, 20929.9, 17652.72, 15980.33,
4500.3, 11477.7, 33754.5, 13539.47, 13539.47,
1819.58, 27740.09, 87476.22, 59670, 1234.56);
Sum = 390135.32;
type
TNode = record
Sum:do
uble;
Used: set of Byte;
d: Integer
end;
var
Stack: array [0..25] of TNode;
Top: Integer;
i: Integer;
begin
Stack[0].Sum:=0;
Stack[0].Used:=[];
Stack[1].d:=0;
Top:=1;
while Top>0do
begin
while Stack[Top].d<25do
begin
Inc(Stack[Top].d);
if not (Stack[Top].d in Stack[Top-1].Used) then
begin
Stack[Top].Sum:=Stack[Top-1].Sum+a[Stack[Top].d];
if Abs(Stack[Top].Sum-Sum)<1e-05 then
begin
for i:=1 to Top-1do
Write(a[Stack.d]:0:2, '+');
WriteLn(a[Stack[Top].d]:0:2)
end
else
if Stack[Top].Sum<Sum then
begin
Stack[Top].Used:=Stack[Top-1].Used+[Stack[Top].d];
Inc(Top);
Stack[Top].d:=Stack[Top-1].d
end
end
end;
Dec(Top)
end;
WriteLn('OK');
ReadLn
end.
可以看看它的相关贴子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2293872