--MaoPaoSort
--[[ local a = {1,2,6,3,6,7,5,34,7}
function MaoPaoSort()
for i = 1, #a - 1 do
for j = 1, #a - i do
if(a[j] > a[j + 1]) then
local temp = a[j]
a[j] = a[j + 1]
a[j + 1] = temp
end
end
end
end
MaoPaoSort(a);
for _,v in pairs(a) do
print(v)
end
--]]
--多返回值,多参数函数
--[[
function Muti(...)
local t = {}
for i,v in pairs{...} do
t[i] = v
end
return t;
end
local a = Muti("first", "second", "third")
for k,v in ipairs(a) do
print(k, v)
end
function Muti2()
local a = 1
local b = 2
local c = 3
local d = 4
return a,b,c,d
end
print(Muti2())
--]]
--斐波那契数列递归和迭代写法
--[[
function Digui(value)
if(value == 1 or value == 2) then
return 1
else
return Digui(value - 1) + Digui(value - 2)
end
end
function DieDai(value)
local a = {};
a[1] = 1
a[2] = 1
for i = 3, 10 do
a[i] = a[i -1] + a[i - 2]
end
return a[value]
end
for i = 1, 6 do
print(Digui(i))
print(DieDai(i))
end
--]]
--99乘法表
--[[
function X99()
local s = ""
for i = 1,9 do
for j = 1, 9 do
if(j <= i) then
s = s .. i .. "*" .. j .. "=" .. i*j .. "|"
end
end
s = s .. "\n"
end
print(s)
end
X99()
--]]
--自己的迭代器,函数闭包
--[[
local a = {1,2,3,4}
function Iter(t)
local cnt = 0
local len = #t
return function()
cnt = cnt + 1
if(cnt <= len) then
return t[cnt]
else
cnt = 1;
return t[cnt];
end
end
end
local f = Iter(a);
print(f())
print(f())
print(f())
print(f())
print(f())
--]]