输入一串数字字符串,读取所有任意个数(参数)组合字符串(200)

  • 主题发起人 主题发起人 gala007
  • 开始时间 开始时间
G

gala007

Unregistered / Unconfirmed
GUEST, unregistred user!
“08,12,21,30,03,19,28,09,13,23”任取3个字符串,如:‘08,12,21’、‘08,12,30’、‘08,13,23’。。。。‘09,13,23’谁能帮我写一个函数,输入字符串和参数n,数字个数。输出所有组合的字符串。
 
procedure TForm1.FormCreate(Sender: TObject);var SL1,SL2:TStringList;
I,J:Byte;
begin
Randomize;
SL1:=TStringList.Create;
SL2:=TStringList.Create;
try SL1.DelimitedText:='08,12,21,30,03,19,28,09,13,23';
SL1.Delimiter:=',';
for I:=1 to 3do
begin
J:=Random(SL1.Count);
SL2.Add(SL1.Strings[J]);
SL1.Delete(J);
end;
Caption:=SL2.DelimitedText;
finally FreeAndNil(SL1);
FreeAndNil(SL2);
end;
end;
 
...implementation{$R *.dfm}var a:array[0..100] of integer;
n:integer;
r:integer=3;//procedure HS(t: integer;AList:TStrings);//用回溯法进行组合var i:Integer;
s:string;
begin
if t>r then
begin
s:=inttostr(a[1]);
for i:=2 to rdo
s:=s+','+IntToStr(a);
AList.Add(s);
end else
begin
for i:=1 to ndo
begin
a[t]:=i;
if (a[t-1]<a[t]) then
HS(t+1,AList);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);var sList,sList2:TStringList;
i,j:integer;
s:string;
begin
sList:=TStringList.Create;
try sList.Delimiter:=',';
sList.DelimitedText:=trim(Edit1.Text);
n:=sList.Count;
Memo1.Clear;
HS(1,Memo1.Lines);
sList2:=TStringList.Create;
try for i:=0 to Memo1.Lines.Count-1do
begin
sList2.Delimiter:=',';
sList2.DelimitedText:=Memo1.Lines;
s:=sList[StrToInt(sList2[0])-1];
for j:=1 to sList2.Count-1do
s:=s+','+sList[StrToInt(sList2[j])-1];
Memo1.Lines:=s;
end;
finally sList2.Free;
end;
finally sList.Free;
end;
end;
 
说错了,应该把全部数字的组合取出来,[:)]
 
谢谢两位,我说错了,应该:取出n个数所有组合的字符串。
 
谢谢,wangdonghai
 
后退
顶部