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

问题描述

倒星型图案由给定数量的星号行组成;其中,下一行的星号要少一个,直到最后一行的星号保持不变;每五个星号都用#替换(请参见图像):

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, '\n')
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, '\n'))

请注意,星形模式的行及其模式本身仅在完成时存储为与 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 函数的替代实现:

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, '\n')
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, '\n')
end

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

09-11 11:20