求一个排列组合的算法(100)

  • 主题发起人 主题发起人 11830
  • 开始时间 开始时间
1

11830

Unregistered / Unconfirmed
GUEST, unregistred user!
求一个函数,实现一下功能。有N个字符,如“abc”要求显示出这N个字符的组合情况,如下。abcdabacbabcabcabcbacbcacabcba
 
http://1zwwz.mygrep.com/show.php?mod=article&id=1888
 
function ResultBuf(N: array of string): string;var i, j, k, K1, x, y, Max, Min: Integer;
s: string;
M: array of string;
begin
Max := High(N);
Min := Low(n);
k := 0;
for i := Min to Maxdo
begin
SetLength(M, Length(M) + 1);
m[Length(M) - 1] := N;
Inc(K);
end;
x := 0;
K1 := 0;
for y := Min to Max - 1do
begin
K1 := 0;
for i := Min + x to Min + x + K - 1do
begin
for j := Min to Maxdo
begin
if pos(N[j], M) = 0 then
begin
SetLength(M, Length(M) + 1);
m[Length(M) - 1] := M + N[j];
Inc(K1);
end;
end;
end;
x := x + k;
k := K1;
end;
for i := 0 to Length(M) - 1do
begin
Result := Result + '|' + M;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);var a, b, c, d: string;
s: string;
begin
a := 'a';
b := 'b';
c := 'c';
d := 'd';
s := ResultBuf([a, b, c,d]);
ShowMessage(s);
end;
改正后的
 
LZ看下是不是这样的 看出来有点问题,改好后在发上来 本楼代码有问题,正确的见楼下function ResultBuf(N: array of string): string;var i, j, k, K1, x, Max, Min: Integer;
s: string;
M: array of string;
begin
Max := High(N);
Min := Low(n);
k := 0;
for i := Min to Maxdo
begin
SetLength(M, Length(M) + 1);
m[Length(M) - 1] := N;
Inc(K);
end;
for x := Min to Maxdo
begin
for i := Min + length(N) * x to K + length(N) * x - 1do
begin
for j := Min to Maxdo
begin
if pos(N[j], M) = 0 then
begin
SetLength(M, Length(M) + 1);
m[Length(M) - 1] := M + N[j];
Inc(K1);
end;
end;
end;
end;
for i := 0 to Length(M) - 1do
begin
Result := Result + '|' + M;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);var a, b, c, d: string;
s: string;
begin
a := 'a';
b := 'b';
c := 'c';
d := 'd';
s := ResultBuf([a, b, c]);
ShowMessage(s);
end;
 
后退
顶部