问题描述
按字符数计数的最短代码,它将根据用户输入生成蜂箱.
The shortest code by character count that will generate a beehive from user input.
一个蜂箱定义为一个六边形网格,用户输入的大小为两个大于零的正数(无需验证输入).第一个数字(W
)表示蜂箱的宽度-或-每行上有多少个六边形.第二个数字(H
)代表蜂箱的高度-或-每列上有多少个六边形.
A beehive is defined a a grid of hexagons in a size inputted by the user as two positive numbers greater than zero (no need to validate input). The first number (W
) represents the width of the beehive - or - how many hexagons are on each row. The second number (H
) represents the height of the beehive - or - how many hexagons are on each column.
一个六边形由三个ASCII字符组成:_
,/
和\
,以及三行:
A Single hexagon is made from three ASCII characters: _
, /
and \
, and three lines:
__
/ \
\__/
六边形彼此完成:蜂箱的第一列将为低",第二列将为高-交替并重复以形成W六边形的相同图案.将重复H次以形成总共WxH个六边形.
Hexagons complete each other: the first column of the beehive will be 'low', and the second will be high - alternating and repeating in the same pattern forming W hexagons. This will be repeated H times to form a total of WxH hexagons.
Input:
1 1
Output:
__
/ \
\__/
Input:
4 2
Output:
__ __
__/ \__/ \
/ \__/ \__/
\__/ \__/ \
/ \__/ \__/
\__/ \__/
Input:
2 5
Output:
__
__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/
Input:
11 3
Output:
__ __ __ __ __
__/ \__/ \__/ \__/ \__/ \__
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
代码计数包括输入/输出(即完整程序).
Code count includes input/output (i.e full program).
推荐答案
Perl,99个字符
@P=map{$/.substr$".'__/ \\'x99,$_,$W||=1+3*pop}0,(3,6)x pop;
chop$P[0-$W%2];print" __"x($W/6),@P
最后编辑:保存了一个字符,将-($W%2)
替换为0-$W%2
(感谢A. Rex)
Last edit: Saved one character replacing -($W%2)
with 0-$W%2
(thanks A. Rex)
说明:
对于宽度W和高度H,输出为2 + 2 * H行长和3 * W + 1个字符宽,在输出的中间有很多重复.
For width W and height H, the output is 2+2 * H lines long and 3 * W+1 characters wide, with a lot of repetition in the middle of the output.
为方便起见,我们将$W
设为3 * W +1,即以字符为单位的输出宽度.
For convenience, we let $W
be 3 * W + 1, the width of the output in characters.
第一行由模式" __"
组成,重复W/2 == $W/6
次.
The top line consists of the pattern " __"
, repeated W/2 == $W/6
times.
偶数行由重复模式"\__/ "
组成,该模式被截断为$W
个字符.输出的第二行是一种特殊情况,其中第二行的第一个字符应为空格而不是\
.
The even numbered lines consist of the repeating pattern "\__/ "
, truncated to $W
characters. The second line of output is a special case, where the first character of the second line should be a space instead of a \
.
奇数行由重复模式"/ \__"
组成,该模式被截断为$W
个字符.
The odd numbered lines consist of the repeating pattern "/ \__"
, truncated to $W
characters.
我们构造一个字符串:" " . "__/ \" x 99
.请注意,此字符串的开头是第二行的期望输出.对于奇数行,此行从位置3开始是期望的输出,对于偶数行,该行从位置6开始.
We construct a string: " " . "__/ \" x 99
. Note that the beginning of this string is the desired output for the second line. This line starting at position 3 is the desired output for the odd lines, and starting at position 6 for the even numbered lines.
map
调用的LIST参数以0开头,后跟(3,6)的H个重复. map
调用创建一个从适当位置开始并且为$W
= 3 * W + 1个字符长的子字符串的列表.
The LIST argument to the map
call begins with 0 and is followed by H repetitions of (3,6). The map
call creates a list of the substrings that begin at the appropriate positions and are $W
= 3 * W + 1 characters long.
在打印结果之前,还需要进行另一项调整.如果W为奇数,则在第二行($P[0]
)上有一个额外的字符,需要将该字符chop
断开.如果W是偶数,则底行($P[-1]
)上还有一个多余的字符可以切.
There is one more adjustment to make before printing the results. If W is odd, then there is an extra character on the second line ($P[0]
) that needs to be chop
ped off. If W is even, then there is an extra character on the bottom line ($P[-1]
) to chop.
这篇关于高尔夫代码:蜂巢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!