下面是一个演示程序:
program Permute ;
{$APPTYPE CONSOLE}
uses SysUtils ;
var
R , Slen : Integer ;
procedure P ( var A : string ; B : string );
var
J : Word ;
C , D : string ;
begin
{ P(N,N) >> R=Slen }
if Length ( B ) = SLen - R then
begin
Write ( ' {' + A + '} ' ); {Per++}
end
else
for J := 1 to Length ( B ) do
begin
C := B ;
D := A + C [ J ];
Delete ( C , J , 1 );
P ( D , C );
end ;
end ;
var
Q , S , S2 : string ;
begin
S := ' ' ;
S2 := ' ' ;
while ( S <> '' ) and ( S2 <> '' ) do
begin
Writeln ( '' );
Writeln ( '' );
Write ( 'P(N,R) N=? : ' );
ReadLn ( S );
SLen := Length ( S );
Write ( 'P(N,R) R=? : ' );
ReadLn ( S2 );
if s2 <> '' then R := StrToInt ( S2 );
Writeln ( '' );
Q := '' ;
P ( Q , S );
end ;
end .