为我的孩子搭建楼梯


25

前几天,我儿子让我用乐高积木为他建造一个楼梯。我想到了这样的事情:

楼梯

然后我的孩子问我一个程序,该程序使用最少的字节数在计算机屏幕上生成了类似的阶梯。我不太擅长于,所以我需要您的帮助。我需要一个程序:

  • 接收带有楼梯所需的级别数的正整数。
  • 输出带有您在图像中看到的图案的楼梯图。

输出将为文本格式,但可以将积木区分开。例如,您可以将'█'字符用作半个字块,并以所需的任何颜色对其进行绘制,或者仅选择您选择的任何字符。

限制条件:

  • 块需要具有三种不同的颜色,并且将使用尽可能长的颜色(如果输入为1或2,则没有足够的块来使用所有三种颜色)。如果需要,可以使用'░▒░'字符,例如,也可以选择三个不同的字符。
  • 同一行中不能有两个相同颜色或图案的块并排放置。

只要画上楼梯,我儿子就不会真正在意尾随空间或换行。

示例(对字符的错误选择表示抱歉):

Input: 1
Output:
██

Input: 2
Output:
██
 ▓▓

Input: 3
Output:
██
 ▓▓
██░░

Input: 5
Output:
██
 ██
██░░
 ██░░
██░░▓▓

17
告诉你的孩子,有趣的是自己构建/编程:-)
Luis Mendo

3
欢迎光临本站!通常不希望将ASCII艺术和图形输出结合起来,因为这会带来两个并行的挑战。另外,如果您选择ASCII,我会允许使用实际的ASCII字符(我想您已经这样做了,但也许更明确了),因为某些语言可能会遇到非ASCII字符的问题
Luis Mendo

2
仅供参考,这些街区称为Duplo
caird coinheringaahing

10
@cairdcoinheringaahing duplo没有圆形按钮。
Christoph

3
@cairdcoinheringaahing问题中的模块不是Duplo,Duplo是Lego兼容的,那些是Lego Baby。他们最初被称为得宝普里莫,我希望他们有一个不同的名称,或者这两个,但他们在技术上不同的产品比两乐高得宝和。
Draco18s

Answers:


5

果冻 21 19  16 字节

d2SR+%3x2⁶;ṙḂµ€Y

完整程序打印结果。

使用001122作为块。

在线尝试!

怎么样?

d2SR+%3x2⁶;ṙḂµ€Y - Main link: number n
             µ€  - for €ach "row" in n (i.e. for row, r = 1 to n inclusive):
d2               -   divmod 2   -> [integer-divide by 2, remainder] i.e. [r/2, r%2]
  S              -   sum        -> r/2 + r%2
   R             -   range      -> [1, 2, 3, ..., r/2 + r%2]
    +            -   add r      -> [r+1, r+2, r+3, ..., r + r/2 + r%2]
     %3          -   modulo 3   -> [r%3+1, r%3+2, r%3+0, ..., (r + r/2 + r%2)%3]
                 -   e.g.: r: 1  , 2  , 3    , 4    , 5      , 6      , 7       , ...
                             [2], [0], [1,2], [2,0], [0,1,2], [1,2,0], [2,0,1,2], ...
       x2        -   times 2 - repeat each twice (e.g. [2,0,1,2] -> [2,2,0,0,1,1,2,2]
         ⁶       -   literal space character
          ;      -   concatenate (add a space character to the left)
            Ḃ    -   r mod 2 (1 if the row is odd, 0 if it is even (1 at the top))
           ṙ     -   rotate (the list) left by (r mod 2)
               Y - join with newlines
                 - implicit print (no brackets printed due to the presence of characters)

11

Python 2,55个字节

i=2
exec"print(i%2*' '+`2%i*1122`*i)[:i];i+=1;"*input()

在线尝试!

的各块之间的循环22, 44,但最上面一行除外00。例如,在输入10上打印

00
 22
2244
 2244
224422
 224422
22442244
 22442244
2244224422
 2244224422

i=2,3,..通过创建奇数长度的空格,重复图案i时间并截短为length来打印长度增加的行i。该模式是2244对除了第一个所有行i=2针对它的0。这是通过算术表达式实现的2%i*1122


1
只使用两种颜色的情况下,3
克里斯托夫

非常聪明的修复!
查理

4

JavaScript(ES6),80个字节

n=>eval(`for(s=11,i=1;i++<n;)s+='\\n'+(' '+'2233'.repeat(n)).substr(i%2,i+1);s`)


JavaScript(ES6),87个字节

先前的解决方案。

n=>[11,...Array(n).fill(' '+'2233'.repeat(n)).map((r,n)=>r.slice(n%2,n+3+n%2))].join`
`

3

SOGL31 28 27 25 字节

∫³2\@*O"²b“2⁵I%*r*;I»«nKp

说明:

∫                          iterate input times, pushing 1-indexed counter
 ³                         get 3 total copies of it on stack
  2\                       1 if divides by 2, else 0
    @*                     get that many spaces
      O                    output in a new line
       "²b“                push 1122
           2⁵I%*           multiply 1122 by 2%(iteration+1)
                r          convert to string
                 *         multiply by iteration
                  ;I»«     get one iteration variable ontop of stack
                      n    increase, floor divide by 2, multiply by 2 (gets the amount of bricks in a line)
                       Kp  split that multiplied string in pieces of that length

使用此技术的
示例输出为9:

00
 22
2244
 2244
224422
 224422
22442244
 22442244
2244224422

非竞争性,22个字节

∫³2\@*O"²b“2⁵I%*;I»«mp

该命令m已在第一次SOGL提交中记录下来,只是未实现。


3

05AB1E22 21 20 18 17字节

使用有趣的事实是 4^(N+2)/5 = [3,12,51,204,...] = b[11,1100,110011,11001100,...]

F4NÌm5÷bDðì}r·IF,

在线尝试!

说明

F                     # for N in 0...input-1 do
 4                    # push 4
  NÌ                  # push N+2
    m                 # push 4^(N+2)
     5÷               # integer division by 5
       b              # convert to binary
        D             # duplicate
         ðì           # prepend a space to the copy
           }          # end loop
            r         # reverse stack
             ·        # multiply top of stack by 2
              IF      # input times do
                ,     # print with newline

抱歉,我改善了答案-不错的方法!
乔纳森·艾伦,

@JonathanAllan:当然可以;)谢谢!用答案中的mod2旋转处理缩进的很酷的想法。
Emigna

2

PHP,61 59

aa<?for(;++$i<$argn;)echo"
",str_pad(" "[~$i&1],2+$i,bbcc);

的工作方式与python版本非常相似,但在可能的情况下使用所有三种颜色。没有尾随新行。

-2 bytes by @user63956. Thanks !

1
您可以使用保存两个字节aa<?for...
user63956 2013年

1

Pyth,29个字节

VQI!%hN2pd)Vh/N2p*2@G%+NH3)pb

在线测试!

说明

VQI!%hN2pd)Vh/N2p*2@G%+NH3)pb

VQ                               For N in range(0, input)
  I!%hN2pd)                      If N is odd, print a leading space
           Vh/N2          )      For H in range(0, N / 2 + 1)
                   @G%+NH3       Select the letter at position (N + H) % 3 in the alphabet
                 *2              Then make it a two letters string ("aa" or "bb" or "cc")
                p                Print it
                           pb    End the line by printing a new line

我确信有很多方法可以缩短该代码,但是我现在很累……稍后再试。


0

批处理,125字节

@set s=█
@for /l %%i in (2,1,%1)do @call:c
:c
@set s= %s:█= %
@set s=%s:▓=█%
@set s=%s:░=▓%
@set s=%s:  =░░%
@echo %s%

注意:将其保存在CP437或CP850等中。每次旋转颜色即可工作。由于无法映射到字符串上来执行旋转,因此我使用了四个替换,将空格用作临时阶段。然后,这还允许我在每行前面加一个空格,以使两个空格变成一个新块。样本输出:

░░
 ▓▓
░░██
 ▓▓░░
░░██▓▓
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.