使用递归算法,最简单
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
TElements = array[1..15] of Integer;
TSelected = array[1..5] of Integer;
TOutputCallBack = procedure (const ret: TSelected);
function GetSel(const Elements: TElements;
Start: Integer;
var Selected: TSelected;
SelCount: Integer;
var RetS: String): Boolean;
var
i, j: Integer;
t: String;
begin
Result := False;
if Start > 15 then
Exit;
if SelCount > 5 then
Exit;
SelCount := SelCount + 1;
for i := Start to 15do
begin
Selected[SelCount] := Elements;
if SelCount = 5 then
begin
for j := 1 to 5do
begin
if j = 1 then
t := IntToStr(Selected[j])
else
t := t + ',' + IntToStr(Selected[j]);
end;
if RetS = '' then
RetS := t
else
RetS := RetS + #13#10 + t;
end;
GetSel(Elements, i + 1, Selected, SelCount, RetS);
end;
end;
{$R *.dfm}
var
m: TElements = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
n: TSelected;
procedure TForm1.Button1Click(Sender: TObject);
var
ms: String;
begin
GetSel(m, 1, n, 0, ms);
Memo1.Lines.Text := ms;
Caption := IntToStr(Memo1.Lines.Count);
end;
end.