-- 2014-1-8
-- wlywly007
-- 测试汉诺塔
local num = 0
local a = "a"
local b = "b"
local c = "c"
local n = 3
function hamoi(n, a, b, c)
num = num + 1
if(n == 1) then
print("将号为" .. n, "的圆盘,从 "..a.." 塔移动到 ".. c .." 塔")
else
hamoi(n - 1, a, c, b)
print("将号为" .. n, "的圆盘,从 "..a.." 塔移动到 ".. c .." 塔")
hamoi(n - 1, b, a, c)
end
end
hamoi(n, a, b, c)
print("总共移动 ".. num .." 次")
-- wlywly007
-- 测试汉诺塔
local num = 0
local a = "a"
local b = "b"
local c = "c"
local n = 3
function hamoi(n, a, b, c)
num = num + 1
if(n == 1) then
print("将号为" .. n, "的圆盘,从 "..a.." 塔移动到 ".. c .." 塔")
else
hamoi(n - 1, a, c, b)
print("将号为" .. n, "的圆盘,从 "..a.." 塔移动到 ".. c .." 塔")
hamoi(n - 1, b, a, c)
end
end
hamoi(n, a, b, c)
print("总共移动 ".. num .." 次")