type
Arrint=array of integer;
ArrintMxN=array of Arrint;
procedure CombinEndless(soure:ArrintMxN;M,N:integer;index:int64;
var tempa:Arrint;var target:ArrintMxN);
var
i:integer;
tmpA: Arrint;
count:integer;
begin
if high(tempa)=-1 then
setlength(tempa,n);
tmpA:=copy(tempa);
if index=n then
begin
count:=high(target)+1;
setlength(target,count+1);
// setlength(target[count-1],m);
target[count]:=copy(tmpA);
exit;
end;
for i := 0 to m-1 do
begin
tmpA[index]:=soure[index];
CombinEndless(soure,M,N,index+1,tmpA,target);
end;
end;
例子:
procedure TForm1.Button6Click(Sender: TObject);
var
i_MxN,R_Mxn:ArrintMxN;
i,j,count:integer;
tmp:Arrint;
st:string;
begin
count:=1;
setlength(i_MxN,8);
for i := 0 to 7 do
begin
setlength(i_MxN,3);
for j := 0 to 2 do
begin
i_MxN[j]:=count;
inc(count);
end;
end;//for i := 0 to 7
CombinEndless(i_MxN,3,8,0,tmp,R_Mxn);
st:='';
memo1.Lines.Clear;
for i := 0 to high(R_Mxn) do
begin
st:=st+inttostr(i)+': ';
for j := 0 to 7 do
begin
st:=st+inttostr(R_Mxn[j])+',' ;
// memo1.Lines.Add(st);
end;
st:=st+#13#10;
end;
memo1.Lines.Text:=st;
end;
结果(部分):
0: 1,4,7,10,13,16,19,22,
1: 1,4,7,10,13,16,19,23,
2: 1,4,7,10,13,16,19,24,
3: 1,4,7,10,13,16,20,22,
4: 1,4,7,10,13,16,20,23,
5: 1,4,7,10,13,16,20,24,
6: 1,4,7,10,13,16,21,22,
7: 1,4,7,10,13,16,21,23,
8: 1,4,7,10,13,16,21,24,
........
6554: 3,6,9,12,15,18,19,24,
6555: 3,6,9,12,15,18,20,22,
6556: 3,6,9,12,15,18,20,23,
6557: 3,6,9,12,15,18,20,24,
6558: 3,6,9,12,15,18,21,22,
6559: 3,6,9,12,15,18,21,23,
6560: 3,6,9,12,15,18,21,24,
正好是3的8次方 6561(0为底)