namespace C4_11
{
public class Program
{
///
///遞迴移動盤子
///
///盤子個數
///A柱
///B柱
///C柱
public static void Hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
move(a, c);
}
else
{
Hanoi(n - 1, a, c, b);
move(a, c);
Hanoi(n - 1, b, a, c);
}
}
public static void move(char sour, char dest)
{
Console.WriteLine("把{0}石柱最上面的盤子移動到{1}石柱上。", sour, dest);
}
public static void Main()
{
int n;
Console.WriteLine("請你輸入漢諾塔盤子的個數:");
n = Convert.ToInt32(Console.ReadLine());
Hanoi(n, 'A', 'B', 'C');
Console.WriteLine("移動完畢。");
}
}
}
沒有留言:
張貼留言