using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hanoi

{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
int n = int.Parse(Console.ReadLine());
p.hanoi(n, 'A', 'C', 'B');
Console.ReadLine();
}
private void hanoi(int n, char s, char d, char t)
{
if (n == 1)
{ Console.Write("{0}->{1}\t", s, d); return; }
hanoi(n - 1, s, t,d);
Console.Write("{0}->{1}\t", s, d);
hanoi(n - 1, t, d,s);
return;
}
}
}
好无聊,看奥运的时候心血来潮,就写下来了。
本文提供了一个使用C#语言解决汉诺塔问题的示例代码。通过递归算法,实现了将不同数量的盘子从一个柱子移动到另一个柱子的过程,并详细打印出了每一步的操作。
620

被折叠的 条评论
为什么被折叠?



