可以参考一下,我以前写的
输入123,输出
1
2
3
12
21
23
32
123
213
312
132
231
321
//////////////////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MPlayer;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure taxis(str: string;
var retStr: TStrings);
{
str: 组合的元素,如'abcd'
retStr: 组合的结果集
}
var
i,j,i1,j1,l: Integer;
s,s1,s2: String;
begin
retStr.Clear;
for i := 1 to length(str)do
begin
for j := 1 to length(str)-i+1do
begin
s1 := copy(str,j,i);
s2 := s1;
repeat
for i1 := 1 to length(s2)do
begin
s := copy(s2,i1,1);
for j1 := 1 to length(s2)do
begin
if (i1<>j1) then
s := s + copy(s2,j1,1);
end;
if retStr.IndexOf(s)<0 then
retStr.Add(s);
end;
l := length(s1);
if l>1 then
s2:=copy(s2,l,1)+copy(s2,1,l-1)
else
until (s1=s2);
end;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aaa:TStrings;
begin
aaa := TStringList.Create;
taxis(edit1.text,aaa);
memo1.Clear;
memo1.Lines.AddStrings(aaa);
end;
end.