Lua论分析需求(学好英文)的重要性

题目是这样的:

 


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)

 

posted @ 2018-12-28 19:00  _清风明月  阅读(271)  评论(0编辑  收藏  举报