打印所需数量的倒​​星图案 [英] Printing the required number of inverted star patterns

查看:26
本文介绍了打印所需数量的倒​​星图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

倒星形图案由给定数量的星号行组成;其中每下一行少一个星号,直到最后一行保留一个星号;每五个星号替换为 #(参见 image):

An inverted star pattern consists of a given number of rows of asterisks; in which each next row has one asterisk fewer, until one asterisk in the last row remains; with every fifth asterisk replaced with a # (see image):

****#*
****#
****
***
**
*

系统会提示用户输入星形图案的数量,然后输入每个图案的宽度(与高度相同).然后输出所需大小的所需图案数.

User is prompted to enter the number of star patterns and then, for each pattern, its width, which is the same as height. Then the required number of patterns of needed size, is to be output.

推荐答案

好吧,直到不使用变量作为限制"部分澄清了,这是

Well, until the part "without using variable as limit" is clarified, it's

-- Localise functions that are called several times, for performance.
local floor = math.floor
local len, sub, rep = string.len, string.sub, string.rep
local insert, concat = table.insert, table.concat

local function pattern (size, template)
    -- Repeat template as many times as necessary to cover size and cut off what is not needed:
    local line = sub (rep (template, floor (size / len (template) ) + 1 ), 1, size)
    local lines = {}
    while (len (line) > 0) do
        insert (lines, line)
        line = sub (line, 1, -2)        -- cut the last character off.
    end
    return concat(lines, '
')
end

io.write ('How many inverted star patterns do you want? ')
local no = tonumber (io.read ()) or 0
local patterns = {}

for i = 1, no do
    io.write ('How many rows in pattern no. ' .. tostring (i) .. '? ')
    patterns[i] = pattern (tonumber (io.read ()) or 0, '****#')
end

print (concat (patterns, '
'))

请注意,星形模式的行和模式本身仅在它们完成时才存储为与 table.concat 连接的表,而不是使用 在任何迭代中附加的字符串...这更快,因为在 Lua 中,每个连接都会重新分配一个字符串.

Note that rows of the star pattern and the patterns themselves are stored as tables that are concatenated with table.concat only when they are complete, rather than as strings appended on any iteration using ... This is faster, since in Lua a string will be re-allocated on each concatenation.

这是pattern函数的另一种实现:

And this is an alternative implementation of the pattern function:

local function pattern (size, template)
    local length = len (template)
    local rows = {}
    for i = size, 1, -1 do
        -- Repeat template as many times as necessary to cover i and cut off what is not needed:
        insert (rows, sub (rep (template, floor (i / length) + 1), 1, i))
    end
    return concat (rows, '
')
end

这是另一种实现,其中模式行被缓存,这可能会加速程序:

And this is another implementation, where pattern rows are cached, which might speed the program up:

local cache = {}
local function pattern (size, template)
    local length = len (template)
    local rows = {}
    for i = size, 1, -1 do
        -- Repeat template as many times as necessary to cover i and cut off what is not needed.
        -- And cache it.
        cache[i] = cache[i] or sub (rep (template, floor (i / length) + 1), 1, i)
        insert (rows, cache[i])
    end
    return concat (rows, '
')
end

这篇关于打印所需数量的倒​​星图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆