在Delphi中怎么实现排列组合
1个回答
2017-01-06 · 知道合伙人软件行家
关注
展开全部
提供排列、组合功能函数的源代码(代码源于网络)如下:
const
BASIC_NUM :Array [0..9] of String = ('0','1','2','3','4','5','6','7','8','9');
//BASIC_OBJ :Array [0..4] of String = ('Red', 'Green', 'Blue', 'White', 'Black');
AMOUNT_NUM :Integer = 10;
//AMOUNT_OBJ :Integer = 5;
PARADE_TYPE :Integer = 0;
COMBINE_TYPE :Integer = 1;
procedure Parade(nBase :Integer; Var sList :TStringList);
var
ssList :TStringList;
nIdx, nSidx :Integer;
begin
ssList := TStringList.Create;
try
if (nBase < 1) then
begin
//do nothing
end else
begin
if (sList.Count = 0) then
begin
for nIdx := 0 to AMOUNT_NUM-1 do
ssList.Add(BASIC_NUM[nIdx]);
end else
begin
for nIdx := 0 to AMOUNT_NUM-1 do
for nSidx := 0 to sList.Count-1 do
ssList.Add(BASIC_NUM[nIdx]+sList.Strings[nSidx]);
end;
Parade(nBase-1, ssList);
end;
if ssList.Count > 0 then
begin
sList.Clear;
sList.Text := ssList.Text;
end;
finally
ssList.Free;
end;
end;
procedure Combine(nBase :Integer; Var sList :TStringList);
var
ssList :TStringList;
nIdx, nSidx :Integer;
begin
ssList := TStringList.Create;
try
if (nBase < 1) then
begin
//do nothing
end else
begin
if (sList.Count = 0) then
begin
//for nIdx := 0 to AMOUNT_OBJ-1 do
for nIdx := 0 to AMOUNT_NUM-1 do
//ssList.Add(BASIC_OBJ[nIdx]);
ssList.Add(BASIC_NUM[nIdx]);
end else
begin
//for nIdx := 0 to AMOUNT_OBJ-1 do
for nIdx := 0 to AMOUNT_NUM-1 do
for nSidx := 0 to sList.Count-1 do
//if (Pos(BASIC_OBJ[nIdx], sList.Strings[nSidx]) = 0) then
if (Pos(BASIC_NUM[nIdx], sList.Strings[nSidx]) = 0) then
//ssList.Add(BASIC_OBJ[nIdx]+','+sList.Strings[nSidx]);
ssList.Add(BASIC_NUM[nIdx]+sList.Strings[nSidx]);
end;
Combine(nBase-1, ssList);
end;
if ssList.Count > 0 then
begin
sList.Clear;
sList.Text := ssList.Text;
end;
finally
ssList.Free;
end;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询