//用组合就可以了 //借用一下二进制unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}uses Math;const cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string; var I, J: Integer; begin Result := ''; I := mNumber; while (I >= mScale) and (mScale > 1) do begin J := I mod mScale; I := I div mScale; Result := cScaleChar[J + 1] + Result; end; Result := cScaleChar[I + 1] + Result; if mLength > 0 then for I := 1 to mLength - Length(Result) do Result := '0' + Result; end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer; var I: Byte; L: Integer; begin Result := 0; L := Length(mDigit); for I := 1 to L do Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) * Trunc(IntPower(mScale, I - 1)); end; { DigitToInt }function f(mPrices: array of Real; mMoney: Real): string; var I, J: Integer; L: Integer; S, C: string; T: Real; begin L := Length(mPrices); with TStringList.Create do try for I := 0 to Trunc(IntPower(2, L)) - 1 do begin S := IntToDigit(I, 2, L); T := 0; C := ''; for J := 1 to L do if S[J] = '1' then begin T := T + mPrices[J - 1]; C := C + ',' + FloatToStr(mPrices[J - 1]); end; System.Delete(C, 1, 1); if T = mMoney then Add(S + ':' + C); end; Result := Text; finally Free; end; end;procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text := f([1, 2, 3, 4, 5, 6, 7], 20); end;end.
//借用一下二进制unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}uses
Math;const
cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string;
var
I, J: Integer;
begin
Result := '';
I := mNumber;
while (I >= mScale) and (mScale > 1) do begin
J := I mod mScale;
I := I div mScale;
Result := cScaleChar[J + 1] + Result;
end;
Result := cScaleChar[I + 1] + Result;
if mLength > 0 then
for I := 1 to mLength - Length(Result) do
Result := '0' + Result;
end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer;
var
I: Byte;
L: Integer;
begin
Result := 0;
L := Length(mDigit);
for I := 1 to L do
Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *
Trunc(IntPower(mScale, I - 1));
end; { DigitToInt }function f(mPrices: array of Real; mMoney: Real): string;
var
I, J: Integer;
L: Integer;
S, C: string;
T: Real;
begin
L := Length(mPrices);
with TStringList.Create do try
for I := 0 to Trunc(IntPower(2, L)) - 1 do begin
S := IntToDigit(I, 2, L);
T := 0;
C := '';
for J := 1 to L do if S[J] = '1' then begin
T := T + mPrices[J - 1];
C := C + ',' + FloatToStr(mPrices[J - 1]);
end;
System.Delete(C, 1, 1);
if T = mMoney then
Add(S + ':' + C);
end;
Result := Text;
finally
Free;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := f([1, 2, 3, 4, 5, 6, 7], 20);
end;end.