static void Main(string[] args)
{
Console.WriteLine("请输入圆盘个数:");
int N = 0;
N = Convert.ToInt32(Console.ReadLine());//强转
hanoi(‘A‘, ‘B‘, ‘C‘, N);
Console.WriteLine();
Console.ReadKey();
}
static void hanoi(char A, char B, char C, int count)
{
if (count == 1)
Console.WriteLine("1: " + A + "->" + B);
else
{
hanoi(A, C, B, count - 1);//从A借助C移动到B
Console.WriteLine(count + ": " + A + "->" + B);
hanoi(C, B, A, count - 1);//从C借助B移动到A
}
}
时间: 2024-10-10 17:36:16