Lua程序设计第4版第14章课后练习答案

14.1

function f141(a,b)
    local c ={}
    for i = 1, #a do
        c[i] = {}
        for j = 1, #a[i] do
            c[i][j]  = a[i][j]+b[i][j]
        end
    end
    for i = 1, #c do
        for j = 1, #c[i] do
            io.write(c[i][j].."\t")
        end
        print()
    end
end
a = {{1,2,3},{4,5,6},{7,8,9}}
b = {{1,2,3},{4,5,6},{7,8,9}}
f141(a,b)

14.2

--f142
function Queue_New()
    return {first = 0,last = 0}
end
function pushFirst(q,value)
    if Empty(q) then
        q.last = q.last-1
    end
    q.first = q.first-1
    q[q.first] = value
end
function popFirst(q)
    if Empty(q) then
        print("nothing to pop")
        return
    end
    local v = q[q.first]
    if q.first==q.last then
        q.last = 0
        q.first = 0
        return v
    end --只有一个元素
    q.first= q.first+1
    return v
end
function pushLast(q,value)
    if Empty(q) then
        q.first = q.first+1
    end
    q.last = q.last+1
    q[q.last] = value
end
function popLast(q)
    if Empty(q) then
        print("nothing to pop")
        return
    end
    local v = q[q.last]
    if q.last==q.first then
        q.last = 0
        q.first = 0
        return v
    end--只有一个元素
    q.last = q.last-1
    return v
end
function Empty(q)
    return q.last==q.first and q.first==0
end
function displayLst(q)
    while Empty(q)==false do
        local v = popFirst(q)
        io.write(v.." ")
    end
end
q = Queue_New()
--pushLast(q,10)
--pushLast(q,11)
--pushLast(q,12)
--pushLast(q,13)
--displayLst(q)
--print(q.first.."-"..q.last)

14.3 14.4

function name2node(graph,name)
    local node = graph[name]
    if not node then
        node = {name = name,adj = {}}
        graph[name] = node
    end
    return node
end

function readgraph(filename)
    local graph = {}
    for line in io.lines(filename) do
        local namefrom,nameto,Label = string.match(line,"(%S+)%s+(%S+)%s+(%d+)")
        local from = name2node(graph,namefrom)
        local to = name2node(graph,nameto)
        temp = {[1] = true,[2] = Label}
        from.adj[to] = temp
    end
    return graph
end

-- a->b 的最短路径
function Dijksra(graph,a,b)
    local shortDis = {} --各节点到a的最短路径集合
    local parent = {}
    local visited = {} --判断节点是否遍历过,遍历过节点为true代表在S集合中,没有遍历过代表为false在V-S集合中
    local cnt = 1
    local total = 0
    -- 初始化parent都为a
    -- 初始化shortDis都为a到各节点的距离
    -- 初始化visited 除了a结点其他结点都未遍历过
    for name,adj in pairs(graph) do
        if name == a.name then
            shortDis[name] = math.huge-1
            parent[name] = -1
            visited[name] = true
        else
            --初始化为a结点直接到b结点的距离
            local node=  name2node(graph,name)
            if a.adj[node] ~=nil then
                -- get node name
                shortDis[node.name] =math.tointeger(a.adj[node][2])
            else
                shortDis[node.name] = math.huge-1
            end
            parent[node.name] = a.name
            visited[node.name] = false
        end
        total = total+1
    end
    while cnt<total do
        -- 获得V-S中的最短路径结点
        local minNode
        local minDis = math.huge
        for name,dis in pairs(shortDis) do
            if visited[name] == false then
                if shortDis[name]<minDis then
                    minDis = shortDis[name]
                    minNode = name2node(graph,name)
                end
            end
        end
        if minNode~=nil then
            visited[minNode.name] = true
        end
        cnt = cnt+1
        -- 更新以minNode结点为父亲的其他最短路径结点
        for name,dis in pairs(shortDis) do
            if visited[name]== false then
                local newNode = name2node(graph,name)
                if minNode.adj[newNode]~=nil then
                    local newDis = math.tointeger(minNode.adj[newNode][2])+minDis
                    if shortDis[name]>newDis then
                        shortDis[name] = newDis
                        parent[name] = minNode.name
                    end
                end
            end
        end
    end

    s = ""
    tmp = b.name
    while tmp~=-1 do
        s = s..tmp
        tmp = parent[tmp]
    end
    s = string.reverse(s)
    print("路径为:"..s)
end

graph =  readgraph("graph")
anode = name2node(graph,"a")
bnode = name2node(graph,"b")
Dijksra(graph,anode,bnode)

恶心我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Lua程序设计第4》是一本非常经典的Lua编程书籍,它介绍了Lua程序设计的基本概念和技巧,并提供了大量的实例和练习,适合初学者和有一定编程基础的人阅读。 这本书的PDF本非常方便,可以在电子设备上随时随地阅读。拥有PDF格式的书籍,读者可以通过搜索、书签、标注等功能,更好地管理和查找自己感兴趣的内容。此外,PDF本还可以进行页面放大、缩小、翻转等操作,适应不同设备和阅读需求。对于学习Lua编程的人来说,这本书的PDF本无疑是很有帮助的。 《Lua程序设计第4》从基础语法、数据类型、运算符等内容开始介绍Lua的基础知识,然后逐步深入到表、函数、模块等高级特性,还介绍了面向对象编程和异常处理等更高级的主。通过阅读这本书,读者可以系统地学习Lua的各种语言特性和编程技巧,掌握Lua编程的基本原理和方法。 在阅读过程中,读者可以参考书中的实例代码进行练习,并通过实践来加深对Lua编程的理解和掌握。此外,书中还提供了一些练习,可以帮助读者巩固所学知识,培养编程思维和解决问的能力。 总之,《Lua程序设计第4》是一本很有价值的Lua编程书籍,提供了全面而深入的学习内容,适合想要学习Lua编程的读者阅读。PDF本的书籍具有便携性和便捷性,非常方便读者随时随地进行学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值