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

本文介绍了一个使用Lua语言编写的程序,该程序能够打印出一个右对齐的楼梯状图案。通过分析楼梯图案中每行#号与空格的关系,作者实现了这一功能。文章提供了两种实现方法,并详细解释了代码的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目是这样的:

 


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)

 

转载于:https://www.cnblogs.com/blackteeth/p/10192346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值