题目是这样的:
Observe that its base and height are both equal to
,and the image is drawn using #
symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size .
Function Description
Complete the staircase function in the editor below. It should print a staircase as described above.
例子是这样的:
What fuck!这是右对齐么???耗尽我的脑细胞,分析每行#号前后需要空格与行数的对应关系。结果只要右对齐就可以了。
1 function InitStr(n ) 2 str = {}-- body 3 for i=1,n do 4 str[i] = {} 5 for j=1,n do 6 str[i][j] = "#" 7 end 8 end 9 return str 10 end 11 12 function staircase1(n) 13 -- body 14 15 arr = InitStr(n) 16 for i=1,n do 17 integer,frac = math.modf((n-i)/2) 18 if(frac > 0)then 19 leftSpaceCount = integer + 1 20 else 21 leftSpaceCount = integer 22 end 23 for j=1,i do 24 arr[i][leftSpaceCount+j] = "#" 25 end 26 print(table.concat(arr[i])) 27 end 28 end 29 30 function staircase(n) 31 -- body 32 33 arr = InitStr(n) 34 for i=1,n do 35 leftSpaceCount = n - i 36 for j=1,leftSpaceCount do 37 arr[i][j] = " " 38 end 39 print(table.concat(arr[i])) 40 end 41 end 42 staircase(6)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。