-
C#教程之c#实现汉诺塔问题示例
汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。下面是c#实现汉诺塔示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 汉诺塔
{
class Program
{
static void hanoi(char A, char B, char C, int count)
{
if (count == 1)
Console.WriteLine("1: " + A + "->" + B);
else
{
hanoi(A, C, B, count - 1);
Console.WriteLine(count + ": " + A + "->" + B);
hanoi(C, B, A, count - 1);
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入圆盘个数:");
int N = 0;
N = Convert.ToInt32(Console.ReadLine());
hanoi('A', 'B', 'C', N);
Console.WriteLine();
Console.ReadKey();
}
}
}