본문 바로가기

Perl

[Perl] hanoi

sub hanoi {

my ($mai , $x, $y, $z) =@_;

if ($mai > 0){

hanoi($mai-1 , $x , $z , $y );

print "move", $mai ,"from", $x ,"to", $z ,"\n";

hanoi($mai-1 , $y , $x , $z );

}

}

hanoi(3, "A", "B", "C");

상기의 결과에 더해, 각행의 선두에 수순의 순번수를 출력하도록 변경해라. 


use strict;

use warnings;


my $i = 0;


sub hanoi {

my ($mai , $x, $y, $z) =@_;

if ($mai > 0){

hanoi($mai-1 , $x , $z , $y );

print ++$i;

print "move", $mai ,"from", $x ,"to", $z ,"\n";

hanoi($mai-1 , $y , $x , $z );

}

}

hanoi(3, "A", "B", "C");


7-g.pl


'Perl' 카테고리의 다른 글

[Perl] 재귀함수  (0) 2014.12.19
[Perl] 홀수 짝수 Subroutine  (0) 2014.12.19
[Perl] Subroutine parameter  (0) 2014.12.19
[Perl] Subroutine  (0) 2014.12.19
[Perl]STDIN, while, for, if  (0) 2014.11.19