Hello!
Tell me what's wrong in the code, I run it - the computer freezes .. I fixed something, but I'm afraid to re-launch something .. This is the task of the Hanoi towers.

use encoding 'cp1251', STDOUT => 'cp866', STDIN => 'cp866'; #! /bin/usr/perl print "Задача о ханойских башнях\n"; print "Задайте число дисков на стержне А (не более 8)\n"; sub Hanoi{ my ( @A , @B , @C ,$n) = @_ ; #print @A ; if ($n == 1){ my ($k) = pop( @A ); push ( @C ,$k); #print "Стержень \ @A : \n"; #print @A ; #print "Стержень \ @B : \n"; # print @B ; #print "Стержень \ @C : \n"; #print @C ; return (print "Снять диск со стержня \ @A и положить на стержень \ @C ;\n"); } else{ Hanoi( @A , @C , @B ,$n-1); print "Cнять диск со стержня \ @A и положить на стержень \ @C \n"; Hanoi( @B , @A , @C ,$n-1); } } $num = <STDIN>; for ($num) { s/^\s+//; s/\s+$//; } $num + 0; @A = (); @B = (); @C = (); for ($i = $num; $i>0;$i--){ push( @A ,$i); print "A[\$i] = $i\n"; } Hanoi( @A , @B , @C ,$num); 

Can any of you dare to run - check whether it works or not ..

  • my (@ A, @ B, @ C, $ n) = @_; Everything that is in @_ will be copied to @A - Error
  • rather than everything in @A, as in the call to Hanoi (@ A, @ B, @ C, $ num); - Uliana_Sever
  • Then pass links to the array, and work with links to the array - Error
  • does not work with links or without them (((what is wrong here ... - Ulyana_Seve
  • Show how you make links - Error

1 answer 1

 use strict; use warnings; use v5.14; use utf8; sub hanoi { my ($a, $b, $c, $n) = @_ ; if($n == 1) { push($c, pop($a)); return print "\ @A -> \ @C \n"; } else { hanoi($a, $c, $b, $n - 1); print "\ @A -> \ @B \n"; hanoi($b, $a, $c, $n - 1); } } my $a = [1, 2, 3, 4]; my $b = []; my $c = []; hanoi($a, $b, $c, 4); 

PS I did not fix your algorithm