2010-09-14から1日間の記事一覧

ハノイの塔(Ruby版)

N = ARGV[0].to_i $s = [[],[],[]] N.downto(1){|i| $s[0] << i } def printstack puts "-----------" (0..2).each{|i| puts "s[#{i}]=[#{$s[i].join(",")}]" } end def movehanoi(f, t, d) puts "movehanoi(#{f}, #{t}, #{d})" tmp = ([0, 1, 2] - [f, t])[…

ハノイの塔(Perl版)

http://twitter.com/tomotaka_ito/status/24445961129 後輩がやってる課題作成のやつをやってみたらハノイの塔をPerlで解くというお題だったので挑戦してみた。 $n = $ARGV[0]; print "N = $n\n"; $s=[[],[],[]]; for($i=$n;1<=$i;$i--){push(@{$s[0]},$i);}…