62,254
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HanoiProgram
{
class Program
{
static long count = 0;
static void move(char x, char y)
{
Console.WriteLine("{0}--->{1}", x, y);
}
static void hanoi(int n, char one, char two, char three)
{
if (n == 1)
{
count += 2;
move(one, two);
move(two, three);
}
else
{
count += 2;
hanoi(n - 1, one, two, three);
move(one, two);
hanoi(n - 1, three, two, one);
move(two, three);
hanoi(n - 1, one, two, three);
}
}
static void Main(string[] args)
{
Console.WriteLine("需要搬几个盘子");
int n = int.Parse(Console.ReadLine());
hanoi(n, 'A', 'B', 'C');
Console.WriteLine("搬迁结速;一共进行了{0}次的搬迁。", count);
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HanoiProgram
{
class Program
{
static long count = 0;
static void move(char x, char y)
{
count += 1;
Console.WriteLine("{0}--->{1}", x, y);
}
static void hanoi(int n, char one, char two, char three)
{
if (n == 1)
{
move(one, two);
move(two, three);
}
else
{
hanoi(n - 1, one, two, three);
move(one, two);
hanoi(n - 1, three, two, one);
move(two, three);
hanoi(n - 1, one, two, three);
}
}
static void Main(string[] args)
{
Console.WriteLine("需要搬几个盘子");
int n = int.Parse(Console.ReadLine());
hanoi(n, 'A', 'B', 'C');
Console.WriteLine("搬迁结速;一共进行了{0}次的搬迁。", count);
Console.Read();
}
}
}