1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
program Permutations;
{ Programme qui produit toutes les permutations d'une chaîne de caractères. }
function Permutation(const s: string; const n: integer): string;
{ Ramène le dernier caractère de la chaîne à la énième place. }
var
i, l: integer;
begin
result := s;
l := Length(result);
result[l - n + 1] := s[l];
for i := l - n + 2 to l do
result[i] := s[i - 1];
end;
procedure PermutationRecursive(var s: string; const n: integer);
{ Permutation récursive des n premiers caractères. }
var
i: integer;
begin
for i := 1 to n do
begin
s := Permutation(s, n);
if n = 1 then
WriteLn(s)
else
PermutationRecursive(s, n - 1);
end;
end;
var
s: string;
begin
s := '1234';
PermutationRecursive(s, Length(s));
end. |
Partager