lua 代码题目

楼梯有n阶台阶,上楼可以一步上1阶,也可以一步上2阶,编一程序列出每一种走法。

递归求解,table的使用

function Way(n)
    local ans = {}

    if n == 1 then

        table.insert(ans,{1})
        return ans

    elseif n == 2 then
        local tmp1 = {1, 1}
        local tmp2 = {2}
        table.insert(ans, tmp1)
        table.insert(ans, tmp2)
        return ans;
    else

        local ans1 = Way(n-1)
        for i , v in pairs(ans1) do
            table.insert( ans1[i], 1)
            table.insert(ans, ans1[i])
        end

        local ans2 = Way(n-2)
        for i , v in pairs(ans2) do
            table.insert( ans2[i], 2)
            table.insert(ans, ans2[i])
        end


        return ans
    end
end


ans = Way(5)

for i,v in pairs(ans) do
    print( "solve " .. i .. ": ".. table.concat(ans[i], "-") )
end

运行截图
这里写图片描述

实现queue数据结构


List = {}

function List.new()
    return {front = 1 , last = 0}
end

function List.push(list,value)
    local last = list.last
    list.last = last + 1
    list[list.last] = value
end

function List.front(list)
    if list.last >= list.front
    then
        local index = list.front
        return list[index]
    else
        return nil
    end
end

function List.pop(list)
    if list.last >= list.front
    then
        local front = list.front
        list[front] = nil
        list.front = front + 1
        return true
    else
        return false
    end
end

function List.size(list)
    if list.last >= list.front
    then
        return list.last - list.front + 1
    else
        return 0
    end
end


local que = List.new()

List.push(que,1)
List.push(que,2)
List.push(que,3)

print(List.size(que)) -- 3

val = List.front(que)
print(val) -- 1

List.pop(que)

print(List.size(que)) -- 2
val = List.front(que)
print(val) -- 2

类和元表的使用

Comp = {real = 0, ima = 0}

Comp.__index = Comp

function Comp:new(o)
    o = o or {}
    setmetatable(o,  self)
    self.__index = self
    return o
end

function Comp:printf()
    print("(" .. self.real .. "," .. self.ima .. ")")
end


meta={
    __add = function(op1,op2)
        op = {}
        op.real = op1.real + op2.real
        op.ima = op1.ima + op2.ima
        return op
    end
}


c1 = Comp:new({real = 1})
c1:printf()                     -- (1,0)
c2 = Comp:new({real=1,ima=2})
c2:printf()                     -- (1,2)


t1 = setmetatable(c1,meta)
t2 = setmetatable(c2,meta)
t = t2 + t2
print(t.real .. ' ' .. t.ima)   -- 2 4

合并两个数组

两个有序数组的合并


-- merge two sorted array
-- note that the start index in lua is 1 not 0
function sortA(a1, a2)
    local len1 = #a1
    local len2 = #a2
    local i = 1
    local j = 1

    local ans ={}

    while i <= len1 and j <= len2 do

        if a1[i] < a2[j] then
            table.insert(ans,a1[i])
            i = i + 1
        elseif a1[i] > a2[j] then
            table.insert(ans, a2[j])
            j = j + 1
        else
            table.insert(ans, a1[i])
            table.insert(ans, a2[j])
            i = i + 1
            j = j + 1
        end
    end

    while i <= len1 do
        table.insert(ans, a1[i])
        i = i + 1
    end

    while j <= len2 do
        table.insert(ans, a2[j])
        j = j + 1
    end

    return ans
end

a1 = {1,5,7,9}
a2 = {1,3,4,6}

ans = sortA(a1,a2)

for i,v in pairs(ans) do
    print(v)
end

function sortA(a1, a2)
    local ans = {}

    for i,v in pairs(a1) do
        table.insert(ans, v)
    end

    for i,v in pairs(a2) do
        table.insert(ans, v)
    end
--[[
    local cmp = function(a,b)
        return a < b
    end

    table.sort(ans, cmp)
]]

    table.sort(ans, function(a,b)
        return a < b
    end)


    return ans
end

a1 = {1,7,5, 9}
a2 = {1,4,3, 6}

ans = sortA(a1,a2)

for i,v in pairs(ans) do
    print(v)
end

求1-n的随机序列


function getRandomList(n)

    local tmp = {}
    for i = 1, n do
        table.insert(tmp, i)
    end

    local ans ={}

    local len = n

    for i = 1, n do
        local pos = math.random(1,len)

        table.insert(ans,tmp[pos]) -- 插入到ans中

        table.remove(tmp, pos) -- tmp删除元素,同时长度减1
        len = len - 1
    end

    return ans
end


r = getRandomList(10)

for i,v in pairs(r) do
    print(v)
end

C++实现随机序列

/*
得到0到n-1的随机序列
*/
vector<int> getRandom(int n)
{
     //拿当前系统时间作为种子,由于时间是变化的,种子变化,可以产生不相同的随机数。
    // <time.h>
    srand((int)time(0));

    vector<int> v(n); // 初始 0, n-1
    for(int i = 0;i < n; i++) 
        v[i] = i;

    int index;
    for(index = 0; index < n; index ++)
    { 
        int randIndex = rand() % n;  

        int tmp = v[index];  
        v[index] = v[randIndex];  
        v[randIndex] = tmp;  
    }

    return v;
}

lua 闭包

闭包实现迭代器


-- 使用闭包实现迭代器
function iter(val)
    local i = 0
    return function()
        i = i + 1
        return val[i]
    end
end

local t = {1, 2, 3, 4}

local it =  iter(t)

while true do

    local element = it()

    if element == nil then
        break
    end

    print(element)

end

参考http://www.cnblogs.com/zzy-frisrtblog/p/5864209.html

#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值