Mình đọc trong sách Giải thuật và lập trình của tác giả Lê Minh Hoàng, mình chạy thử code bằng fpc thì lỗi exitcode 2, ideone thì runtime error 2
program Permutation;
const
InputFile = 'PERMUTE.INP';
OutputFile = 'PERMUTE.OUT';
max = 12;
var
n, i, k, a, b: Integer;
x: array[1..max] of Integer;
f: Text;
procedure Swap(var X, Y: Integer);
var
Temp: Integer;
begin
Temp := X; X := Y; Y := Temp;
end;
begin
Assign(f, InputFile); Reset(f);
Readln(f, n);
Close(f);
Assign(f, OutputFile); Rewrite(f);
for i := 1 to n do x[i] := i;
repeat
for i := 1 to n do write(x[i],' ');
WriteLn(f);
i := n - 1;
while (i > 0) and (x[i] > x[i + 1]) do Dec(i);
if i > 0 then
begin
k := n;
while x[k] < x[i] do Dec(k);
Swap(x[k], x[i]);
a := i + 1; b := n;
while a < b do
begin
Swap(x[a], x[b]);
Inc(a);
Dec(b);
end;
end;
until i = 0;
Close(f);
end.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?