两个矩阵相加,稀疏还不会
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)
队列实现
--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)
图不会
556

被折叠的 条评论
为什么被折叠?



