# 构建ASCII梯形图

28

``````o---o
|   |
|   |
|   |
+---+
|   |
|   |
|   |
+---+
|   |
|   |
|   |
o---o
``````

``````o-o
| |
+-+
| |
+-+
| |
+-+
| |
+-+
| |
o-o
``````

``````o-----o
|     |
|     |
|     |
|     |
|     |
+-----+
|     |
|     |
|     |
|     |
|     |
o-----o
``````

• 长度（n）代表梯子由多少个正方形组成。

• 尺寸（）表示每个正方形的内部宽度和高度，即不包括“边界”。

• 每个正方形均由内部空间组成，内部空间由 `-`顶部和底部的`|`s，左侧和右侧的s以及`+` s和所有四个角的s。

• 正方形之间的边界合并在一起，因此连续两行 `+--...--+`合并为一条。

• 整个梯子的拐角被替换为字符 `o`

• 您可以选择输出尾随换行符。

RK。

@RK。您可以按照更方便的顺序购买它们。

1

1
@CᴏɴᴏʀO'Bʀɪᴇɴ呃...我要去那一个。
Doorknob

1

4

# Pyth，34个字节

``````.NjjNm*QTvz*2YjC:M++J"+|o"m"- -"QJ
``````

11

# Ruby，71岁

``````->m,n{h=0;(?o+?+*(n-1)+?o).chars{|c|puts [?|+' '*m+?|]*h,c+?-*m+c;h=m}}
``````

``````f=->m,n{
h=0                             #The number of | above the 1st rung is 0
(?o+?+*(n-1)+?o).chars{|c|      #Make a string of all the rung ends o++...++o and iterate through it
puts [?|+' '*m+?|]*h,         #draw h vertical segments |  ...  |
c+?-*m+c                    #and a rung with the correct ends
h=m                           #The number of | above all rungs except the 1st is m
}
}

f[gets.to_i,gets.to_i]
``````

manatwork 2013年

@manatwork oops，谢谢，已修复。我不知道那是怎么回事，我一定打过高尔夫球，以后再不跑了。

9

# CJam，43岁 42字节

``````q~:Z;'-'o{[\Z*1\$N]}:X~['-_'+X\'|XZ*]@*1>1\$
``````

``````2 3
o---o
|   |
|   |
|   |
+---+
|   |
|   |
|   |
o---o
``````

``````q~:Z;'-'o{[\Z*1\$N]}:X~['-_'+X\'|XZ*]@*1>1\$
:Z;                                      e# Record the size in Z and discard
'-'o{[\Z*1\$N]}:X~                     e# Create the initial line (and final). also creates a shorcut to do this later
\                               e# Capture two arguments
Z*                             e# The separator is repeated size times
1\$                           e# Repeat the first argument
e# X is a function to create line in a ladder
['-_'+X\'|XZ*]       e# Design the repeating part
@*     e# Repeat the pattern n times
1\$ e# Since the final line is same than the initial, we just write it.
e# Implicit printing
``````

1

undergroundmonorail

7

# JavaScript（ES6），89

...重复，重复，重复...

``````(n,m,R=x=>x.repeat(m),b=R(`|\${R(' ')}|
`),d=`o\${c=R('-')}o
`)=>d+R(b+`+\${c}+
`,m=n-1)+b+d``````

``````F=(n,m,R=x=>x.repeat(m),b=R(`|\${R(' ')}|
`),d=`o\${c=R('-')}o
`)=>d+R(b+`+\${c}+
`,m=n-1)+b+d

// Less golfed
U=(n,m)=>
{
var R=x=>x.repeat(m),
a=R(' '),
b=R(`|\${a}|\n`);
c=R('-'),
d=`o\${c}o\n`;
m=n-1;
return d+R(b+`+\${c}+\n`)+b+d
}

function test() {
var i=I.value.match(/\d+/g)
if (i) O.textContent=F(+i[0],+i[1])
console.log(i,I.value)
}

test()``````
``````N,M: <input id=I value="3,5" oninput=test()>
<pre id=O></pre>``````

F. Hauri

2
@ F.Hauri几乎可在所有浏览器中使用，但应避免使用（除非出于娱乐目的而编写）。信息和链接stackoverflow.com/questions/3434278/...
edc65

6

# C＃，1412字节

...我的第一个CodeGolf尝试，不太可能获胜，但可以成功，所以我们开始：

``````using System;

{
class Program
{
static void Main(string[] args)
{
int n = 0;
int m = 0;

Console.Write("o");
for (int i = 0; i < m; i++)
{
Console.Write("-");
}
Console.WriteLine("o");

for (int k = 0; k < n; k++)
{
for (int i = 0; i < m; i++)
{
Console.Write("|");
for (int j = 0; j < m; j++)
{
Console.Write(" ");
}
Console.WriteLine("|");
}
if (k != n - 1)
{
Console.Write("+");
for (int i = 0; i < m; i++)
{
Console.Write("-");
}
Console.WriteLine("+");
}
}

Console.Write("o");
for (int i = 0; i < m; i++)
{
Console.Write("-");
}
Console.WriteLine("o");

}
}
}
``````

9

Downgoat

user48538 '16

RedLaser

3

RedLaser，2016年

2

6

# 朱莉娅87字节

``f(n,m)=(g(x)=(b=x[1:1])x[2:2]^m*b*"\n";(t=g("o-"))join([g("| ")^m for i=1:n],g("+-"))t)``

``````function f(n::Int, m::Int)
# Create a function g that takes a string of two characters and
# constructs a line consisting of the first character, m of the
# second, and the first again, followed by a newline.
g(x) = (b = x[1:1]) * x[2:2]^m * b * "\n"

# Assign t to be the top and bottom lines. Construct an array
# of length n where each element is a string containing the
# length-m segment of the interior. Join the array with the
# ladder rung line. Concatenate all of this and return.
return (t = g("o-")) * join([g("| ")^m for i = 1:n], g("+-")) * t
end``````

5

# pb -147字节

``````^t[B]>>[B]vw[T!0]{b[43]<[X]b[43]>w[B=0]{b[45]>}v[X-1]w[B=0]{b[124]^}v[X]t[T-1]}t[111]b[T]<w[X!0]{b[45]<}b[T]w[Y!0]{w[B!0]{^}b[124]^}b[T]^>>[B]vb[T]
``````

``````^t[B]            # Save length to T
>>[B]v           # Go to X=size+1, Y=0

w[T!0]{          # While T is not 0:
b[43]            # Write a '+'
<[X]b[43]        # Write a '+' on the left side as well
>w[B=0]{b[45]>}  # Travel back to the right '+', writing '-' on the way
v[X-1]           # Go down by X-1 (== size)
w[B=0]{b[124]^}  # Travel back up to the '+', writing '|' on the way
v[X]             # Go down by X (== size + 1, location of next '+')
t[T-1]           # Decerement T
}

t[111]           # Save 'o' to T (it's used 4 times so putting it
# in a variable saves bytes)

b[T]             # Write an 'o' (bottom right)

<w[X!0]{         # While not on X=0:
b[45]<           # Travel left, writing '-' on the way
}

b[T]             # Write an 'o' (bottom left)

w[Y!0]{          # While not on Y=0:
w[B!0]{^}        # Skip nonempty spaces
b[124]           # Write a '|'
^                # Travel up
}

b[T]             # Write an 'o' (top left, replaces existing '+')

^>>[B]v          # Go back to where the size is saved and go to X=size+1, Y=0

b[T]             # Write an 'o' (top right, replaces existing '+')
``````

5

### 纯bash中，132个130 128 127字节

``````p=printf\ -v;\$p a %\$1s;\$p b %\$2s;o="|\$a|\n";h=+\${a// /-}+\\n v=\${a// /\$o}
a=\${b// /\$h\$v}\${h//+/o};a=\${a/+/o};\${p% *} "\${a/+/o}"
``````

``````ladders() {
p=printf\ -v;\$p a %\$1s;\$p b %\$2s;o="|\$a|\n";h=+\${a// /-}+\\n v=\${a// /\$o}
a=\${b// /\$h\$v}\${h//+/o};a=\${a/+/o};\${p% *} "\${a/+/o}"
}

o---o
|   |
|   |
|   |
+---+
|   |
|   |
|   |
+---+
|   |
|   |
|   |
+---+
|   |
|   |
|   |
o---o

o--o
|  |
|  |
o--o
``````

4

``````l#s=unlines\$t:m++[t]where _:m=[1..l]>>["+"!"-"]++("|"!" "<\$u);t="o"!"-";o!i=o++(u>>i)++o;u=[1..s]
``````

``````*Main> putStr \$ 4 # 3
o---o
|   |
|   |
|   |
+---+
|   |
|   |
|   |
+---+
|   |
|   |
|   |
+---+
|   |
|   |
|   |
o---o
``````

``````l#s=unlines\$t:m++[t]         -- concat top line, middle part and end line
-- with newlines between every line
where                      -- where
_:m=                       -- the middle part is all but the first line of
[1..l]>>                -- l times
["+"!"-"]           --    a plus-dashes-plus line
++("|"!" "<\$u)      --    followed by s times a bar-spaces-bar line

t="o"!"-"                  -- very first and last line
o!i=o++(u>>i)++o           -- helper to build a line
u=[1..s]
``````

-1分的模式匹配`m=init\$[1..l]>>("|"!" "<\$u)++["+"!"-"]`=>`(_:m)=[1..l]>>["+"!"-"]++("|"!" "<\$u)`
Akangka '16

Akangka

@ChristianIrwan：很好发现！谢谢！
nimi 2016年

3

# Brainfuck-334字节

``````,[<+<<<<+>>>>>-]<[[>>]+[<<]>>-]<----[>---<----]--[>[+>>]<<[<<]>++++++]>[+.>>]-[<+>---]<+++++++>>--[<+>++++++]->---[<------->+]++++++++++[<++<]+++++[>[++++++>>]<<[<<]>-]>[-]>.-<<----[>>+++<<----]--[>+<--]>---<<<<++++++++++.,[>[>+>+<<-]>[<+>-]>[<<<<[>>>>>>[.>>]<<[<<]>>-]>>>>>[.>>]<<[<<]>-]<<<<+>-]>>>>[-]----[>---<----]>+.[>]<<<<<[.<<]
``````

``````,[<+<<<<+>>>>>-]<[[>>]+[<<]>>-] Get m from input; make a copy
Turn it into m cells containing 1 with empty cells between

<----[>---<----]      Put 67 at the beginning (again with an empty cell between)

--[>[+>>]<<[<<]>++++++]  Add 43 to every nonempty cell

>[+.>>]               Add 1 to each cell and print it

-[<+>---]<+++++++    Put 92 after the last 45 (no empty cell!)

>>--[<+>++++++]      Put 43 immediately after the 92

->---[<------->+]    Put 234 after 43

++++++++++           And 10 after that

[<++<]             Add two to the 234; 92; the empty spaces; and left of the 111

+++++[>[++++++>>]<<[<<]>-] Add 30 to each 2; the 94; and the 236

>[-]>.-<<----[>>+++<<----] Erase leftmost 32; Print 111; subtract 68 from it

--[>+<--]>---        Put 124 where the 32 was

<<<<++++++++++.,     Print a newline; override the cell with n from input

[                    n times:

>[>+>+<<-]>[<+>-]    Make a copy of m

>[                   m times:

<<<<                 Look for a flag at a specific cell

[                    If it's there:

>>>>>>[.>>]          Go to the 43; print it and every second cell after

<<[<<]>>-            Clear the flag

]

>>>>>[.>>]           Go to the 124; print it and every second cell after

<<[<<]>              Go back to the copy of m

-]

<<<<+>               Plant the flag

-]

>>>>

[-]----[>---<----]>+ Erase the 124; add 68 to 43

.[>]                 Print it; then head to the end

<<<<<[.<<] Go to the last 45; print it; then print every second cell to the left
``````

3

# PowerShell的77 80

``````param(\$l,\$s)\$a='-'*\$s
(\$c="o\$a`o")
((\$b=,"|\$(' '*\$s)|"*\$s)+"+\$a+")*--\$l
\$b
\$c
``````

2

# Jolf，36个字节

``````ρpi,a+2J+2J"o-| "j"o(.+)o
o.+o'+\$1+
``````

## 说明

``````ρpi,a+2J+2J"o-| "j"o(.+)o\no.+o'+\$1+
pi              j                   repeat vertically j times
,a+2J+2J"o-| "                    a box with dimensions 2+J
ρ                 "o(.+)p\np.+o'     replace with regex
+\$1+ with the -...-
``````

2

# Perl，98个字节

``````(\$n,\$m)=@ARGV;print\$h="o"."-"x\$m."o\n",((("|".(" "x\$m)."|\n")x\$m.\$h)x\$n)=~s{o(-+)o(?=\n.)}{+\$1+}gr
``````

1

Level River St

ZILjr'1

1

## C，122字节

``f(int m,int n,char*s){int i=0,w=3+m++;for(;i<w*m*n+w;++i)*s++=i%w>m?10:" |-+-o"[!(i/w%m)*2+!(i%w%m)+!(i/w%(m*n))*2];*s=0;}``

1

# Tcl，187个字节

``````lassign \$argv n w
set c 0
while { \$c < [expr {(\$w * \$n) + (\$n + 2)}]} {if {[expr {\$c % (\$n + 1)}] == 0} {puts "o[string repeat "-" \$w ]o"} else {puts "|[string repeat " " \$w ]|"}
incr c}
``````

1

# PHP，81字节

``````\$R=str_repeat;echo\$P="o{\$R('-',\$W=\$argv[1])}o
",\$R("|{\$R(' ',\$W)}|
\$P",\$argv[2]);
``````

0

## Python 2，94字节

``def F(n,m):a,b,c,d='o|+-';r=[a+d*m+a]+([b+' '*m+b]*m+[c+d*m+c])*n;r[-1]=r[0];print'\n'.join(r)``

'无高尔夫球'：

``````def F(n,m):
# 'o---o'
r = ['o'+'-'*m+'o']
# ('|   |'*m+'+---+') n times
r += (['|'+' '*m+'|']*m+['+'+'-'*m+'+'])*n
# replace last +---+ with o---o
r[-1] = r[0]
print '\n'.join(r)``````

0

# Perl 5，91 +1（-a）= 92字节

``\$_='|'.\$"x\$F[1].'|';push@a,y/| /+-/r,(\$_)x\$F[1]while\$F[0]--;\$a[0]=y/| /o-/r;say for@a,\$a[0]``

0

# 点`-l`，35字节

``````(^YsXbRLaJW'-)XbWR^('|XbRLaJ'+)WR'o
``````

### 说明

``````(^YsXbRLaJW'-)XbWR^('|XbRLaJ'+)WR'o
a is length, b is size, s is space (implicit)
sXb                               String containing b spaces
RLa                            List containing a copies of that string
JW'-                        Join on "-" and wrap the result in "-" as well
Y                                  Necessary for operator precedence reasons
^                                   Split into a list of characters
(            )Xb                     String-repeat each character in the list b times
This list represents the central columns of the ladder

'|Xb             String containing b pipe characters
RLa          List containing a copies of that string
J'+       Join on "+"
(          )WR'o  Wrap in "o"
^                  Split into a list of characters
This list represents the outer columns of the ladder

WR                   Wrap the left list in the right list, vectorizing
``````

### 其他一些版本

``````[Y'-XbWR'o;@>(sXbWR'|RLbPE'-XbWR'+RL:a)y]  41
Y^(t**b.1*:t**bX--a.1)" --"@yXbWR"|o+"@y   40
Y'|XbRLaJ'+YyWR'o;Z:sXbRLaJW'-RLbPEyAEy    39
t**:b(" |-o-+"<>2)@_@^t.1M\$*Y[ttXa-1].1    39
J*Z:sXbRLaJW'-RLbWR:^('|XbRLaJ'+)WR'o      37
Y^\$*Y[t**:btXa-1].1" --"@yXbWR"|o+"@y      37
``````

``````        b           Size; e.g. 3
t               Preset variable for 10
**:            Set t to t**b (e.g. 1000)
a        Length; e.g. 3
-1      2
tX         String-repeat (the new value of) t 2 times: 10001000
[          ]     Put t and the above into a list: [1000; 10001000]
.1   Append 1 to both of them: [10001; 100010001]
\$*(              )  Fold on multiplication: 1000200020001
``````