高分求2维数组组合排列算法!!!(100分)

  • 主题发起人 主题发起人 xuhao1
  • 开始时间 开始时间
X

xuhao1

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个3×N的2维数组,例如:
1, 2, 3
4, 5, 6
7, 8, 9
10, 11, 12
13, 14, 15
.......

如何得到所有的这样的排列组合,
1,4,7,10,13
1,4,7,10,14
1,4,7,10,15
1,4,7,11,13
1,4,7,11,14
1,4,7,11,15
1,4,7,12,13
1,4,7,12,14
1,4,7,12,15
1,4,8,10,13
1,4,8,10,14
1,4,8,10,15
1,4,8,11,13
1,4,8,11,14
1,4,8,11,15
1,4,8,12,13
1,4,8,12,14
1,4,8,12,15
1,4,9,12,10
.......
 
可看看Delphi自带的线程这个例子.(有排序算法的)
根据这个例子改动一下就可以实现了.
 
不是排序
 
可以考虑用递归算法
 
那位高手可以写下源码
 
说下思路也行
 
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为底)
 
接受答案了.
 
后退
顶部