看不到树木的森林


29

编写一个程序或函数来绘制一棵树,从而构建一个森林。

树木被画得像堆积金字塔。第一行(顶部)包含1树,下一行包含2(总计3),下一行包含3(总计6),依此类推。如果没有足够的树木来完成整行,请将其填充到左侧,将右侧的点留空。此外,较低级别的树由于其位置而略微重叠于较高级别的树。

这是一片森林 1

  /\
 //\\
///\\\
  ||
  ||

这是一片森林 2

      /\
     //\\
  /\///\\\
 //\\ ||
///\\\||
  ||
  ||

这是一片森林 3

      /\
     //\\
  /\///\\\/\
 //\\ || //\\
///\\\||///\\\
  ||      ||
  ||      ||

这是一片森林 4

          /\
         //\\
      /\///\\\/\
     //\\ || //\\
  /\///\\\||///\\\
 //\\ ||      ||
///\\\||      ||
  ||
  ||

这是一片大小的森林5(请注意,第五棵树的顶部覆盖了第一棵树的树干)

          /\
         //\\
      /\///\\\/\
     //\\ || //\\
  /\///\\\/\///\\\
 //\\ || //\\ ||
///\\\||///\\\||
  ||      ||
  ||      ||

(跳过一些)
这是一个大小森林8(扩展模式)

              /\
             //\\
          /\///\\\/\
         //\\ || //\\
      /\///\\\/\///\\\/\
     //\\ || //\\ || //\\
  /\///\\\/\///\\\||///\\\
 //\\ || //\\ ||      ||
///\\\||///\\\||      ||
  ||      ||
  ||      ||

等等。

输入值

任何方便格式的一个正整数,n > 0

输出量

遵循上述规则,以森林的ASCII艺术形式表示。前导/尾随的换行符或其他空格是可选的,前提是所有树都适当排列。

规则

  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

我不确定树的绘制顺序与模式有关。也就是说,给定n的树的位置是什么?
路易斯·门多

@LuisMendo据我了解,它们按阅读顺序排列。因此,每一行依次被填充,如果整行没有足够的树木,则其余的树应尽可能地放在该行的最左侧。
xnor

@LuisMendo xnor正确。如果我可以重新输入文字以使其更清楚,请在聊天中对我执行ping操作。
AdmBorkBork

@xnor谢谢,我现在很清楚
Luis

@Adm实际上是在挑战中写的。显然我看不懂:-)
Luis Mendo

Answers:


5

Haskell 310字节

w i=putStr$unlines$reverse$b i 0 0[][]
b 0 _ _ w r=e w r
b c l 0 w r=b c(l+1)l(e w r)[]
b c l p w r=b(c-1)l(p-1)w(n(++)["  ||    ","  ||    ","///\\\\\\  "," //\\\\   ","  /\\    "]r)
e w r=t++n(n d)(map(\t->"    "++t)w)c where(t,c)=splitAt 2 r
n f(a:c)(b:d)=f a b:n f c d
n _ a[]=a
n _ _ a=a
d d ' '=d
d _ d=d

以命名w 5,例如。

这里是未压缩的代码:

-- TreeTree
-- by Gerhard
-- 12 February 2017

module TreeTree (wood,test) where

type Tree = [String]

-- Test cases
test = do
 wood 0
 wood 1
 wood 2
 wood 3
 wood 4
 wood 5

-- build wood
wood :: Int -> IO ()
wood i = printTree $ buildWood i 0 0 [] []

-- Prints the trees
printTree :: Tree -> IO ()
printTree = putStr . unlines . reverse

-- build wood
buildWood :: Int -> Int -> Int -> Tree -> Tree -> Tree
buildWood 0 _ _ w r = concatTree w r 
buildWood c l 0 w r = buildWood c (l+1) l (concatTree w r) []
buildWood c l p w r = buildWood (c-1) l (p-1) w (addTree r)

-- indent definition
space :: String
space = "    "

-- tree definition
tree :: Tree
tree = reverse [
 "  /\\    ",
 " //\\\\   ",
 "///\\\\\\  ",
 "  ||    ",
 "  ||    "]

-- Add a Tree on the left side
addTree :: Tree -> Tree
addTree = match (++) tree

-- add tree row at the bottom of the wood
concatTree :: Tree -> Tree -> Tree
concatTree w r = trunk ++ matched
 where
  wood = grow w
  (trunk, crown) = splitAt 2 r 
  matched = matchTree wood crown

-- elnarge forrest on the left side to match next tree line
grow :: Tree -> Tree
grow = map (\t -> space ++ t)

-- match
match :: (a -> a -> a) -> [a] -> [a] -> [a]
match f (a:az) (b:bz) = f a b : match f az bz
match _ a [] = a
match _ _ a  = a

-- match trees
matchTree :: Tree -> Tree -> Tree
matchTree = match matchLine

-- match lines
matchLine :: String -> String -> String
matchLine = match matchChar

-- match chars
matchChar :: Char -> Char -> Char
matchChar c ' ' = c
matchChar _ c   = c

-- End

欢迎来到PPCG!
AdmBorkBork

4

的JavaScript(ES6),357个 297 276字节

f=
n=>{a=`  /\\`;d=`///\\\\\\`;b=d+`/\\`;c=` //\\\\ ||`;d+=`||`;e=`
`;r=`repeat`;s=``;for(i=1;n>i;n-=i++)s=(s+a+b[r](i-1)+e+c[r](i)).replace(/^/gm,`    `)+e;return(s+a+b[r](n-1)+d[r](i-=n)+e+c[r](n)+(s=`      ||`[r](i))+e+d[r](n)+s+(s=e+`  ||    `[r](n))+s).replace(/\|.$/gm,``)}
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

编辑:感谢@KritixiLithos,节省了21个字节。


对于第一个repeat,您可以更改blah.repeat(val)blah[w="repeat"](val),然后可以将后续出现的更改为repeat[w](val)而不是保存字节
Kritixi Lithos

@KritixiLithos我不能完全做到这一点,因为第一个repeatfor循环内并且不会运行n=1,但是我仍然能够节省21个字节。
尼尔

4

C ++(在Windows),330个 312 308 304 303字节

#import<cstdio>
#import<windows.h>
#define P(x,y,s)SetConsoleCursorPosition(GetStdHandle(-11),{X+x,Y+y});puts(s);
int X,Y,R,r,c;t(){P(2,-2,"/\\")P(1,-1,"//\\\\")P(0,0,"///\\\\\\")P(2,1,"||")P(2,2,"||")}f(int n){for(c=R=r=1;c<n;c+=++R);for(;r;r++)for(c=0;++c<r+1;){X=(R-r-2)*4+c*8;Y=r*2;t();r=--n?r:-1;}}

致电:

int main()
{
    f(8);
}

0

C(在Windows上),297个 295 294字节

#import<windows.h>
#define P(x,y,s)C.X=X+x;C.Y=Y+y;SetConsoleCursorPosition(GetStdHandle(-11),C);puts(s);
COORD C;X,Y,R,r,c;t(){P(2,-2,"/\\")P(1,-1,"//\\\\")P(0,0,"///\\\\\\")P(2,1,"||")P(2,2,"||")}f(n){for(c=R=r=1;c<n;c+=++R);for(;r;r++)for(c=0;++c<r+1;){X=(R-r-2)*4+c*8;Y=r*2;t(r=--n?r:-1);}}

与我的C ++答案相似,但我发布了此内容,因为它在C语言中略短。


@DLosc C. #import是(不建议使用的)GCC扩展名。不过,适合打高尔夫球。
Steadybox'2

嗯,有趣。现在,我看到了一个提示。您可能会在答案中提到这一点。
DLosc

@DLosc也许,但我认为它与其他一些GCC(尽管不限于GCC)扩展一起在高尔夫运动中得到了广泛使用,例如省略<stdio.h>并自动假定一个全局变量为be int或一个要返回的函数int
Steadybox'2

0

JavaScript的418 377个字节

感谢@Kritixi Lithos帮助打高尔夫球39个字节

x=>{s='';for(t=0;++t<x;x-=t);q='//\\\\';z="///\\\\\\";h="/\\";t--;for(i=0;i<t;i++){a=4*(t-i)+1;s+=" "[w="repeat"](a+1)+h+(z+h)[w](i)+`
`+" "[w](a)+q+(" || "+q)[w](i)+`
`}c=t-x+1>0?t-x+1:0;return x?s+"  "+(h+z)[w](--x)+h+(c?(z+"||")[w](c-1)+z:'')+`
 `+q+(" || "+q)[w](x)+" ||     "[w](c)+`
`+(z+"||")[w](x)+z+(c?"||"+"      ||"[w](c-1):'')+`
`+("  ||    "[w](x+1)+`
`)[w](2):''}

在线尝试


2
对于第一个repeat,您可以更改blah.repeat(val)blah[w="repeat"](val),然后可以将随后出现的更改为,repeat而只是[w](val)保存字节
Kritixi Lithos
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.