使用lua螺旋列印陣列

2021-06-23 02:18:31 字數 1122 閱讀 5554

今天在網上看到乙個有意思的題目,如何螺旋列印乙個陣列,比較好奇,就自己先用遞迴實現了一下。

local randomarray = ,

[2] = ,

[3] = ,

[4] =

}for i,v in pairs(randomarray) do

if type(v) == "table" then

for i,v in pairs(v) do

endend

end--邊界條件:#tb = 1

function printaroundchars(tb)

if tb and type(tb) == "table" then

if #tb > 0 then

if #tb == 1 then

print("only one line")

else

local w = #tb[1]

local h = #tb

local leftx = 1

local topy= 1

local xcursor = 1

local ycursor = 1

for i = 1,w do

print(tb[1][i])

endfor i = 2,h do

print(tb[i][w])

endfor i = w-1,1,-1 do

print(tb[h][i])

endfor i = h-1,2,-1 do

print(tb[i][1])

endlocal nexttable = {}

for i = 2,h-1 do

for j = 2,w-1 do

if not nexttable[i-1] then

nexttable[i-1]={}

endnexttable[i-1][j-1] =tb[i][j]

endend

printaroundchars(nexttable)

endend

endendprintaroundchars(randomarray)

目前看來,這個還只是實現了,有沒有更加高效的演算法呢,容我想想 ~~~~

螺旋列印陣列

螺旋列印 include stdafx.h include include include int getcreatematrix int nvectors void spinnerprint int matrix,int n,int nvectors int main int argc,char ...

python 列印陣列

def hex print x for i in range 0,len x print 0x x ord x i 如果沒有print後面的那個逗號,每列印乙個就會換行,加了逗號以後,就後面加空格輸出 字串 str 轉換為列表 list d k.encrypt data list1 list d h...

陣列螺旋列印

package su.interview import utils.com.matrixer 螺旋列印數列 author toy public class array 01 j i 南 system.out.println b i j for int k 0 k n 1 k i j 西 system...