可以把你测试的代码贴出来吗?我测试时感觉速度还好啊。下面是改进的,50个数当中取8个数相加结果为40,运行1秒钟出结果。uses Math;procedure TForm1.Button1Click(Sender: TObject);Const N=50;
M=8;
X=40;Var NumArr:array [0..N-1] Of Integer;
K:Integer;
MinNum,MaxNum
ouble;
Function GetNextNum(StarIndex,Count:Integer;
NeedVal:Integer;
LastStr:String):Boolean;
Var K2:Integer;
begin
K2:=StarIndex;
While K2<= (N-Count)do
begin
if Count=1 then
begin
IF NumArr[K2]=NeedVal then
begin
System.Delete(LastStr,1,1);
Memo1.Lines.Add( LastStr+','+IntToStr(NumArr[K2]) );
end;
end else
begin
if ((Count-1)*MinNum < (NeedVal-NumArr[K2])) and ( (Count-1)*MaxNum > (NeedVal-NumArr[K2]) ) then
GetNextNum( K2+1, Count-1, NeedVal-NumArr[K2], LastStr+','+IntToStr(NumArr[K2]) );
end;
Inc(K2);
end;
end;
begin
For K:=LOW(NumArr) TO High(NumArr)do
NumArr[K]:=K+1;
MinNum:= NumArr[0];
MaxNum:= NumArr[0];
For K:=LOW(NumArr) TO High(NumArr)do
begin
MinNum:= Min(MinNum,NumArr[K]);
MaxNum:= Max(MaxNum,NumArr[K]);
end;
GetNextNum( 0, M, X, '');
caption:=datetimetostr(now);
end;