0
点赞
收藏
分享

微信扫一扫

C#递归实例 汉诺塔

sunflower821 2022-07-14 阅读 95


using System;

namespace ConsoleApp1
{
class Program
{
static int number = 0;
static void Move(char source,char destination)
{
Console.WriteLine("from {0} to {1}",source,destination);
}
static void haobi(int n,char a,char b,char c)
{
number++;
if (n == 1)
{
Move(a, c);
}
else
{
haobi(n - 1, a, c, b);
Move(a, c);
haobi(n - 1, b, a, c);
}
}

static void Main(String[] args)
{
haobi(3, 'a', 'b', 'c');
Console.WriteLine("spent {0} times", number);
}
}
}


举报

相关推荐

0 条评论