using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hanoi
{
class Program
{
static int count = 0;
static void Main(string[] args)
{
Console.WriteLine("请输入圆盘数目:");
int n=int.Parse(Console.ReadLine());
hanoi(n, 'A', 'B', 'C');
Console.WriteLine("计算量是{0}", count);
Console.ReadKey();
}
static void move(char a, char b) //move function o(1)
{
count++;
b = a;
}
static void hanoi(int n, char a, char b, char c) //hanoi function T(n)=2T(n-1)-1
{
if (n == 0)
{
count++;
}
else
{
hanoi(n - 1, a, c, b);
move(a, c);
hanoi(n - 1, b, a, c);
}
}
}
hanoi递归算法
最新推荐文章于 2023-09-26 21:16:36 发布