楼梯有n阶台阶,上楼可以一步上1阶,也可以一步上2阶,编一程序列出每一种走法。
--[[--------------------------------------------------------------------------
表复制:深复制
--]]--------------------------------------------------------------------------
function table.copy(t)
local t2 = {};
for k,v in pairs(t) do
if type(v) == "table" then
t2[k] = table.copy(v);
else
t2[k] = v;
end
end
return t2;
end
--[[--------------------------------------------------------------------------
格式化:将表转为字符串
用于格式化输出
--]]--------------------------------------------------------------------------
function table.formatout(t, h)
local out = "";
for k,v in pairs(t) do
if type(v) == "table" then
if k == 1 then
for i = 1, h do out = string.format("%s%s", out, " "); end
out = string.format("%s%s", out, "{\n");
end
out = string.format("%s%s", out, table.formatout(v, h+1));
if k == table.getn(t) then
for i = 1, h do out = string.format("%s%s", out, " "); end
out = string.format("%s%s", out,"},\n");
end
else
if k == 1 then
for i = 1, h do out = string.format("%s%s", out, " "); end
out = string.format("%s%s", out, "{");
end
out = string.format("%s%d,", out, v);
if (k == table.getn(t)) then
out = string.format("%s%s",out,"},\n");
end
end
end
return out;
end
--[[--------------------------------------------------------------------------
格式化输出
--]]--------------------------------------------------------------------------
function table.print(t)
local out = table.formatout(t, 0);
--去掉最后的",\n"两个字符
print(string.sub(out, 1, string.len(out)-2));
end
--[[--------------------------------------------------------------------------
@param
solution_new @format {{1,2,1} {2,1,1} {1,1,1,1}}
@meaning 插入值后的新解
solution @format {{1,2} {2,1} {1,1,1}}
@meaning 特定n值下,所有的解,每一个元素为一种解
value @format 整数
@meaning 需要插入的值,如1 2
@exmple
输入状态:
solution_new == {}
solution == {{1,2} {2,1} {1,1,1}}
value == 1
运行函数后:
solution_new = {{1,2,1} {2,1,1} {1,1,1,1}}
--]]--------------------------------------------------------------------------
function update_solution(solution_new, solution, value)
local str = string.format("%d", value) --将数值转为字符
table.foreach(solution, function(idx,ele) table.insert(ele, str) table.insert(solution_new, ele) end)
end
--[[
solution : n级阶梯所有可行解 {{}, {}, {}}
]]--
function f_digui(n)
local solution = {}
if n == 1 then
solution = {{1}}
elseif n == 2 then
solution = {{1,1},{2}}
else
solution = {}
update_solution (solution, f_digui(n-2), 2);
update_solution (solution, f_digui(n-1), 1);
end
return solution
end
--[[
solution : 1到n级阶梯所有可行解 {{{}, {}, {}}, {{}, {}, {}} }
solution[i] :i级阶梯所有可行解 {{}, {}, {}}
从这里可以看到,迭代需要使用更多的空间来保存数据
]]--
function f_diedai(n)
local solution = {}
for i_n = 1, n do
if i_n == 1 then
solution[i_n] = {{1}}
elseif i_n == 2 then
solution[i_n] = {{1,1},{2}}
else
solution[i_n] = {}
update_solution (solution[i_n], table.copy(solution[i_n-2]), 2);
update_solution (solution[i_n], table.copy(solution[i_n-1]), 1);
end
end
return solution[n]
end
--[[
n依次从starti变化到endi
输出每个n值的组合
]]--
function test(starti, endi, fuc)
for i = starti, endi, 1 do
local array={}
print()
print("n的值:",i)
if fuc == "digui" then
table.print(f_digui(i))
else
table.print(f_diedai(i))
end
end
end
----------------------------------------------------------------------------------
--test(3,4, "digui");
--test(1,4, "diedai");
--table.print({{{1,2},{3,4}},{{1,2},{3,4}}});
out = "Hello lua!\n这是第一个lua代码,用来解决上楼梯问题\n有几个不错的地方:\n1、递归和迭代的转换\n2、表的复制和表的输出\n3、一些基本语法的使用";
print(out);
test(4, 4, "diedai");
----------------------------------------------------------------------------------
--[[
所有变量默认是全局变量
局部变量需要使用local
]]--
--[[
lua中的函数参数是“引用”
即:在函数中改变该参数的值,会导致该变量改变
]]--
--[[
(代码规范性——作为参考)
一般来说,所有编程语言的函数都不应该返回有意义的值
需要得到的结果通过参数保存
例如在update_solution函数中,我们需要得到的是solution_new
将它作为一个参数,而不要采用return solution_new的形式
返回一般是错误代码,用于错误控制,表示该函数是否正常执行
如果需要返回有意义的值,函数名应命名为get_***
或者,若不返回有意义的值,会使得函数难以理解,也可以返回该值
例如在递归函数f_digui当中
]]--
lua解上阶梯问题(按指定格式输出具体走法、迭代、递归),布布扣,bubuko.com
原文地址:http://blog.csdn.net/hsylx1992/article/details/37671863