macintosh_cookbook/examples/towerofhanoi.pas
2020-05-07 18:47:11 +09:00

24 lines
468 B
ObjectPascal

program TowersOfHanoi (input, output);
var
disks: Integer;
procedure Hanoi (source, temp, destination: char; n: integer);
begin
if n > 0 then
begin
Hanoi(source, destination, temp, n - 1);
Writeln('Move disk ', n : 1, ' from peg ', source, ' to peg ', destination);
Hanoi(temp, source, destination, n - 1);
end;
end;
begin
Write('Enter the number of disks: ');
Readln(disks);
Writeln('Solution:');
Hanoi('A', 'B', 'C', disks);
end.