[C# 演算法]-河內塔

[C# 演算法]-河內塔

[C# 演算法]-河內塔<HanoiTower>

剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,並且轉換成C# Code,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:

http://openhome.cc/Gossip/AlgorithmGossip/

http://openhome.cc/Gossip/AlgorithmGossip/HanoiTower.htm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hanoi
{
class Program
{
/*
程式原始出處:http://openhome.cc/Gossip/AlgorithmGossip/HanoiTower.htm
*/
public static void hanoi(int n, char A, char B, char C)
{
if (n == 1)
{
Console.Write("Move sheet from {0} to {1}\n", A, C);
}
else
{
hanoi(n - 1, A, C, B);
hanoi(1, A, B, C);
hanoi(n - 1, B, A, C);
}
}
static void Main(string[] args)
{
int n;
string data;
Console.Write("請輸入盤數:");
data = Console.ReadLine();
n = System.Convert.ToInt32(data);
hanoi(n, 'A', 'B', 'C');
Console.Write(".......");
Console.ReadKey();
}
}
}

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *