Ruby,111 99 77 73 68 64 57 56字节
-12个字节要感谢Benjamin Urquhart,-43个要感谢manatwork,-2个字节要感谢Value Ink。
->i{s=[];puts (0...i).map{|j|s=(p=' '*j)+?/,*s;p+?\\},s}
在线尝试!
说明:
f=->i{ # instead of a function, use a lambda
s=[] # needs a helper variable *now*, for scope
puts( # puts takes arbitrary num of args; \n after each
(0...i).map{|j| # not from 0 to i but from 0 to i-1 (*three* dots)
s=(
p=' '*j # p will remain in scope inside of .map,
)
+?/ # character literal instead of string
,*s # essentially appending to the array
p+?\\ # p is what's returned by .map, not s!
}, # up until here, 1st arg to display
s # NOW, as the *2nd* arg, s is displayed
)
}
替代(但更长)的解决方案
一个朋友读了这个答案,然后尝试提出另外两种方法。也将它们放在此处,以免它们被庞大的互连网所迷失。
注入和取消移位,72字节
->n{puts (0...n).inject([]){|s,i|i=' '*(n-1-i);s.unshift i+?\\;s<<i+?/}}
在线尝试!
向下,注入和取消移位,80字节
->n{puts n.downto(1).map{|i|' '*(i-1)}.inject([]){|s,i|s<<i+?/;s.unshift i+?\\}}
在线尝试!
有趣的两个非嵌套循环,127个字节
->n{
r=->s,c{s[0..-(c+1)],s[-c..-1]=s[c..-1],s[0..c-1];s};
n.times{|i|puts r[' '*n+?\\,n-i]}
n.times{|i|puts r[' '*n+?/,i+1]}
}
在线尝试!