群里有人出的小题目,觉得有意思,做了下,不要见笑,呵呵。
一个画图程序
要求打印出
int i=5;
1
2 3 4 5
16 17 18 19 6
15 24 25 20
7
14 23 22 21 8
13 12 11 10 9
int
i=6
1 2 3 4
5 6
20 21 22 23 24 7
19 32 33 34
25 8
18 31 36 35 26 9
17 30 29 28
27 10
16 15 14 13 12
11
俺的想法很简单,绕着找到了规律,除了第一行,每一圈中(顺时针),四条边,值递增,位置分别
增+len,-1,-len,+1,其中len是边长,每两边之后递减。
function circle(len) local square = {}
local pos = 0 local count = 0
for i = 1, len do pos = pos + 1 count = count + 1 square[pos] = count end
local dir = 1
for i = len - 1, 1, -1 do for j = 1, i do pos = pos + dir * len count = count + 1 square[pos] = count end
for j = 1, i do pos = pos - dir count = count + 1 square[pos] = count end
dir = dir * -1 end
--[[ for i, v in pairs(square) do io.write(i, '-', v, "t") end
os.exit() --]]
for i = 1, len * len do io.write(square[i], "t") if 0 == i % len then io.write("n") end end end
circle(5) print() circle(6)
|
阅读(1266) | 评论(0) | 转发(0) |