namespace DataStructure
{
class Recursion
{
static void Main(string[] args)
{
int n = 0;
Console.Write("Please input number => ");
try {
n = Convert.ToInt16(Console.ReadLine()); // 讀入數字
}
catch (Exception ex) { // Argument is optional, no "When" keyword
Console.WriteLine(ex.Message);
Console.Read();
}
if(n>15){
Console.WriteLine("The calucation time will be too long to wait.....");
Console.WriteLine("Press Enter key to Exit");
Console.Read();
}
else if (n < 0)
{
Console.WriteLine("input error, number must > 0"); ///小於零之數不正當
Console.WriteLine("Press Enter key to Exit");
Console.Read();
掃數移動次數 = 2^n - 1
}
else
Hanoi(n, "A", "B", "C");
Console.WriteLine("Press Enter key to Exit");
Console.Read();
}
// 把 n 個盤子,從 form 柱 翻譯公司經由 by 柱 翻譯公司搬往 to 柱
public static void Hanoi(int n , String from 翻譯公司 String by 翻譯公司 String to)
{
if(n > 0)
{
Hanoi(n - 1, from, to, by);
Console.WriteLine("move no. {0} disk from {1} to {2}" 翻譯公司 n, from, to);
Hanoi(n - 1, by, from, to);
}
}
}
}
法則:
河內塔目 翻譯:
1. 一次只能移動一個盤子。。-> 翻譯社|,-> 翻譯公司|的-> 翻譯
2 搬運過程當中,大盤子不克不及置於小盤子上方。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
程式碼如下:
// 遞迴 : 河內塔問題 (Towers of Hanoi)
// hanoi() 把 n 個盤子 翻譯公司從 form 柱,經過 by 柱 翻譯公司搬往 to 柱
// 作者: Chris Huang
// 程式語言: VC# 2008 Expression Edition
引用自: http://mypaper.pchome.com.tw/middlehuang/post/1321728075有關翻譯的問題歡迎諮詢天成翻譯社