ハノイの塔 をたくさんの言語で。
唐突に書きたくなって、頑張って色々書いてみた。(残念ながらスタック型言語(Brainf*ckとかwhitespaceとかは無い)
ハノイの塔回答プログラムのポイントはおおよそ2点
- 関数/サブルーチン等の再帰呼び出し処理
- 経過を出力する際の文字列フォーマッティング
それでは、どうぞ
C
#include <stdio.h>
void hanoi(int n, char *x, char *y, char *z)
{
if ( !n ) return;
hanoi(n-1, x, z, y);
printf("move %d from %s to %s\n", n, x, y);
hanoi(n-1, z, y, x);
}
int main()
{
hanoi(4, "a", "b", "c");
return 0;
}
C++
#include <iostream>
template<int N, char X, char Y, char Z>
struct Hanoi
{
void operator()()
{
Hanoi<N-1,X,Z,Y>()();
std::cout << "move " << N << " from " << X << " to " << Y << "\n";
Hanoi<N-1,Z,Y,X>()();
}
};
template<char X, char Y, char Z>
struct Hanoi<0, X, Y, Z>
{ void operator()(){} };
int main()
{
Hanoi<4, 'a', 'b', 'c'>()();
return 0;
}
Perl
sub hanoi {
my ($n, $x, $y, $z) = @_;
return if $n == 0;
hanoi($n - 1, $x, $z, $y);
print "move $n from $x to $y\n";
hanoi($n - 1, $z, $y, $x);
}
hanoi(qw/4 a b c/);
Python
def hanoi(n, x, y, z):
if n == 0:
return
hanoi(n - 1, x, z, y)
print "move %d from %s to %s" % (n, x, y)
hanoi(n - 1, z, y, x)
hanoi(4, 'a', 'b', 'c')
Ruby
def hanoi n, x, y, z
return if n == 0
hanoi n-1, x, z, y
print "move #{n} from #{x} to #{y}\n"
hanoi n-1, z, y, x
end
hanoi 4, 'a', 'b', 'c'
Java (J2SE 5 or 6)
public class hanoi
{
public static void main(String[] args)
{
new hanoi().hanoi(4, "a", "b", "c");
}
void hanoi(int n, String x, String y, String z)
{
if ( n == 0 )
return;
this.hanoi(n - 1, x, z, y);
System.out.printf("move %1$d from %2$s to %3$s%n", n, x, y);
this.hanoi(n - 1, z, y, x);
}
}
C#, VB.net
using System;
namespace hanoi
{
class Hanoi
{
public static void Main(string[] args)
{
new Hanoi().ExecHanoi(4, "a", "b", "c");
}
void ExecHanoi(int n, string x, string y, string z)
{
if ( n == 0 )
return;
this.ExecHanoi(n - 1, x, z, y);
Console.WriteLine("move {0} from {1} to {2}", n, x, y);
this.ExecHanoi(n - 1, z, y, x);
}
}
}
Module Program
Sub Main()
Hanoi(4, "a", "b", "c")
End Sub
Sub Hanoi(n As Integer, x As String, y As String, z As String)
If n = 0 Then
Return
End If
Hanoi(n - 1, x, z, y)
Console.WriteLine("move {0} from {1} to {2}", n, x, y)
Hanoi(n - 1, z, y, x)
End Sub
End Module
sh(Bash)
function hanoi() {
( # 変数をローカルにするためサブシェル化
N=$1; shift
if [ $N -gt 0 ]; then
N1=`expr $N - 1`
X=$1; Y=$2; Z=$3
hanoi $N1 $X $Z $Y
echo "move $N from $X to $Y"
hanoi $N1 $Z $Y $X
fi
)
}
hanoi 4 a b c
MS-DOS Batch
@echo off
CALL :HANOI 4 a b c
GOTO :END
:HANOI
SETLOCAL
IF %1==0 GOTO :ENDHANOI
SET /a N1=%1 - 1
CALL :HANOI %N1% %2 %4 %3
echo move %1 from %2 to %3
CALL :HANOI %N1% %4 %3 %2
:ENDHANOI
ENDLOCAL
EXIT /b
:END
XSL
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:call-template name="hanoi">
<xsl:with-param name="n" select="4" />
<xsl:with-param name="x" select="'a'" />
<xsl:with-param name="y" select="'b'" />
<xsl:with-param name="z" select="'c'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="hanoi">
<xsl:param name="n" as="xs:integer" />
<xsl:param name="x" as="xs:string" />
<xsl:param name="y" as="xs:string" />
<xsl:param name="z" as="xs:string" />
<xsl:if test="$n > 0">
<xsl:call-template name="hanoi">
<xsl:with-param name="n" select="$n - 1" />
<xsl:with-param name="x" select="$x" />
<xsl:with-param name="y" select="$z" />
<xsl:with-param name="z" select="$y" />
</xsl:call-template>
<xsl:text>move </xsl:text>
<xsl:value-of select="$n" />
<xsl:text> from </xsl:text>
<xsl:value-of select="$x" />
<xsl:text> to </xsl:text>
<xsl:value-of select="$y" />
<xsl:text>
</xsl:text>
<xsl:call-template name="hanoi">
<xsl:with-param name="n" select="$n - 1" />
<xsl:with-param name="x" select="$z" />
<xsl:with-param name="y" select="$y" />
<xsl:with-param name="z" select="$x" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Scheme (Gauche)
(define (hanoi n x y z)
(when (> n 0)
(hanoi (- n 1) x z y)
(format #t "move ~s from ~s to ~s~%" n x y)
(hanoi (- n 1) z y x)
)
)
(hanoi 4 'a 'b 'c)
HSP
goto *main
#deffunc hanoi int n, str x, str y, str z
if n=0 : return
hanoi n - 1, x, z, y
mes "move " + n + " from " + x + " to " + y
hanoi n - 1, z, y, x
return
*main
hanoi 4, "a", "b", "c"
なでしこ (cnakoモード)
●ハノイ(xとyとzをnで)
もし、n=0ならば、戻る。
xとzとyをn-1でハノイする。
表示用文字列とは文字列
表示用文字列にnを追加して、
表示用文字列に「を」を追加して、
表示用文字列にxを追加して、
表示用文字列に「から」を追加して、
表示用文字列にyを追加して、
表示用文字列に「へ移動する。」を追加する。
表示用文字列を表示する。
zとyとxをn-1でハノイする。
戻る。
「a」と「b」と「c」を4でハノイする。
以上!とりあえず15言語でのハノイの塔でした。
その他の言語での回答があれば是非
2008-11-14T14:30 追記: あれ?15だと思ったら14しかないんでやんの。。 何か入れ忘れたのかなぁ...





コメントする