主题:[原创]汉诺塔(C#的递归算法)
private void Form7_Load(object sender, EventArgs e)
{
int num = Convert.ToInt32(textBox1.Text);
char B='1'; char C='2';char D='3';
Tower(num, B, C, D);
}
/// <summary>
/// 汉诺塔算法
/// </summary>
/// <param name="a">要移动的盘子的数量</param>
/// <param name="b">盘子开始放置的树桩</param>
/// <param name="c">盘子要被移到的树桩</param>
/// <param name="d">用做临时存放处的树桩</param>
public void Tower(int a, char b, char c, char d)
{
if (a == 1)
{
textBox2.Text += b + "->" + d + " \n";
}
else
{
Tower(a-1,b,d,c);
textBox2.Text+=b+"->"+d+" \n";
Tower (a-1,c,b,d);
}
}
{
int num = Convert.ToInt32(textBox1.Text);
char B='1'; char C='2';char D='3';
Tower(num, B, C, D);
}
/// <summary>
/// 汉诺塔算法
/// </summary>
/// <param name="a">要移动的盘子的数量</param>
/// <param name="b">盘子开始放置的树桩</param>
/// <param name="c">盘子要被移到的树桩</param>
/// <param name="d">用做临时存放处的树桩</param>
public void Tower(int a, char b, char c, char d)
{
if (a == 1)
{
textBox2.Text += b + "->" + d + " \n";
}
else
{
Tower(a-1,b,d,c);
textBox2.Text+=b+"->"+d+" \n";
Tower (a-1,c,b,d);
}
}