昨天的货箱用了非递归的,今天就用个递归的吧.
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TIntegerArr = array [1..4] of Integer;
var
PopedList: TIntegerArr;
Stack: TIntegerArr;
procedure TryDoIt(PopedIndex, UnPushIndex, StackTop: Integer;
Stack: TIntegerArr);
var
i: Integer;
begin
if PopedIndex=5 then
begin
for i:=1 to 4do
Write(Chr(Ord('a')-1+PopedList), ' ');
WriteLn;
Exit
end;
if StackTop>0 then
begin
PopedList[PopedIndex]:=Stack[StackTop];
TryDoIt(PopedIndex+1, UnPushIndex, StackTop-1, Stack)
end;
if UnPushIndex<=4 then
begin
Stack[StackTop+1]:=UnPushIndex;
TryDoIt(PopedIndex, UnPushIndex+1, StackTop+1, Stack)
end;
end;
begin
TryDoIt(1, 1, 0, Stack);
ReadLn
end.
另外,任何结果字符串的子串也是合法串.
这样也行:
procedure TForm1.Button1Click(Sender: TObject);
const
Max = 3 + Ord('a');
var
AIntArray1: array[Ord('a')..Max, Ord('a')..Max, Ord('a')..Max, Ord('a')..Max] of Boolean;
i, j, k, l: Integer;
Flag: Boolean;
begin
Flag := False;
for i := Ord('a') to Maxdo
for j := Ord('a') to Maxdo
for k := Ord('a') to Maxdo
for l := Ord('a') to Maxdo
if (l <> k) and (l <> j) and (l <> i) and
(k <> j) and (k <> i) and (j <> i) then
begin
AIntArray1[j][k][l] := True;
if (j < i) and (j < k) then
if (k < i) then
Flag := True;
if (j < i) and (j < l) then
if l < i then
Flag := True;
if (K < j) and (k < l) then
if l < j then
Flag := True;
if Flag then
AIntArray1[j][k][l] := False;
if AIntArray1[j][k][l] then
Memo1.Lines.Add(Char(i) + ' ' + Char(j) + ' ' +
Char(k) + ' ' + Char(l));
Flag := False;
end;
end;