小时候,我很喜欢玩这些玩具

他们可能打算把这些用在艺术上,但我总是用在数学上!分形,模式等。有一次,我遇到了这样的挑战:
在不使用任何绿色三角形瓷砖的情况下构建一个三角形。
这个挑战让我困惑了很长一段时间,直到我偶然发现了一种非常漂亮和简单的方法,用三个梯形来完成这个任务:
/\
/_/\
/__\_\现在,取这个三角形,旋转它:
______
\ \__/
\/ /
\/ 利用这两个三角形,我们可以用它们构造更大的三角形。这里是一个高度为2的三角形:
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\ 下面是3-7高度的三角形:
#3
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\
/\ \__/\ \__/\
/_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\
#4
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\
/\ \__/\ \__/\
/_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\
#5
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\
/\ \__/\ \__/\
/_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\
#6
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\
/\ \__/\ \__/\
/_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\/__\_\
#7
/\
/_/\
/__\_\
/\ \__/\
/_/\/ /_/\
/__\_\/__\_\
/\ \__/\ \__/\
/_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\/__\_\
/\ \__/\ \__/\ \__/\ \__/\ \__/\ \__/\
/_/\/ /_/\/ /_/\/ /_/\/ /_/\/ /_/\/ /_/\
/__\_\/__\_\/__\_\/__\_\/__\_\/__\_\/__\_\挑战
编写一个程序或函数,该程序或函数采用n个数字,并打印高度n为n的无三角形三角形。每一行上的尾随空格是可以接受的,最多一个尾随或前导换行符也是可以接受的。IO可以是任何合理的格式。输入被保证为正整数,所以你不必担心负数,小数,非数字等等。
最短答案(以字节为单位)获胜!
发布于 2016-04-14 16:59:01
ri_"/__\_\/_/\/ /\ \__"6/f**eeW%{_S.*s\~,\-<N}/ri_ read the input, convert to integer and duplicate
"…" push that string, containing the repeating pattern
(3 lines in reverse order, concatenated)
6/ split into (3) lines of 6 characters
f* multiply (repeat) each line n times
* repeat the array of 3 lines n times
at this point we have an array of 3*n strings with 6*n characters each
ee enumerate the array (obtaining an array of [index string] pairs)
W% reverse the array
(so the lines are in correct order and indices in reverse order)
{…}/ for each [index string] pair
_ duplicate the pair
S.* vectorized-multiply with " "
this effectively replaces the index with a string of <index> spaces
s convert the pair to string, effectively concatenating the spaces
with the string
\ swap with the other copy of the [index string] pair
~, dump the index and string on the stack and get the string length
\- subtract the index from it - this is the desired line length
< cut the concatenated string to that length
N add a newline在网上试试
发布于 2016-04-14 16:38:36
->n{1.upto(n*=3){|i|puts (' '*(n-i)).ljust(n+i,'/__\_\/\ \__/_/\/ '[i%3*6,6])}}A. (-4字节,-1 +1)从0索引(.times)变为1索引(1.upto)
B. (-5字节)从三个6字符字符串的数组改为选择18字符字符串的6字符子字符串。
C. (-1字节) m=n*3 -> n*=3
D. (-5个字节)将所有五个双反斜杠减少为单个反斜杠(部分原因是重新排序A点所需的字符串)。
->n{(m=n*3).times{|i|puts (' '*(m-i-1)).ljust(m+i+1,[ '/\\ \\__','/_/\\/ ','/__\\_\\'][i%3])}}基本单元是一个3x6菱形,如下所示(为清晰起见,每行上的最后一个字符重复:)
/\ \__/
/_/\/ /
/__\_\/我们所需要做的就是显示这个模式的一个合适的窗口。Ruby的ljust允许您使用任何字符串,而不仅仅是空格。通常,ljust将通过在末尾添加空格来填充可打印字符字符串,但在这里我们使用它反向使用:通过在末尾添加可打印字符来填充一串空格。
中未使用
f=->n{
(m=n*3).times{|i| #for each line of the triangle
puts (' '*(m-i-1)). #print m-i-1 spaces,
ljust(m+i+1,[ '/\\ \\__', #left justified and padded to total length m+i+1
'/_/\\/ ', #by one of these three strings
'/__\\_\\'][i%3])
}
}
f[gets.to_i]发布于 2016-04-14 20:19:22
n=>`,/\\ \\__,/_/\\/ ,/__\\_\\`[r=`repeat`](n).split`,`.map((s,i)=>` `[r](n*3-i)+s[r](n).slice(0,i*2)).slice(1).join`\n`其中\n表示文字换行符。如果带有n*3空格和换行符的前导行是可以接受的,那么.slice(1)可以删除,节省9个字节。
https://codegolf.stackexchange.com/questions/77782
复制相似问题