English 中文(简体)
Golf
原标题:Code Golf: Beehive
  • 时间:2009-10-08 19:50:12
  •  标签:
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

The challenge

最短的代码按特性计算,将产生用户投入的粘合剂。

用户投入的电量被定义为六角电网,其数值大于零(无需验证投入)。 第一个数字(W)表示原样的宽度,或者说,每行有多少六角。 第二个编号(H)是粘合层的高度,或是每栏有多少六角。

单六角由三种ASCII特性组成:_,/>。

 __
/  
\__/

六角各行各样:原封顶的第一栏为低级,第二栏为高级——在构成六角的同一模式中相互改变和重复。 这将反复出现,形成总共六角的WxH。

Test cases:

Input:
    1 1
Output:
     __
    /  
    \__/

Input:
    4 2
Output:
        __    __
     __/  \__/  
    /  \__/  \__/
    \__/  \__/  
    /  \__/  \__/
    \__/  \__/

Input:
    2 5
Output:
        __ 
     __/  
    /  \__/
    \__/  
    /  \__/
    \__/  
    /  \__/
    \__/  
    /  \__/
    \__/  
    /  \__/
    \__/

Input:
    11 3
Output:
        __    __    __    __    __
     __/  \__/  \__/  \__/  \__/  \__
    /  \__/  \__/  \__/  \__/  \__/  
    \__/  \__/  \__/  \__/  \__/  \__/
    /  \__/  \__/  \__/  \__/  \__/  
    \__/  \__/  \__/  \__/  \__/  \__/
    /  \__/  \__/  \__/  \__/  \__/  
    \__/  \__/  \__/  \__/  \__/  \__/

法典包括投入/产出(即全部方案)。

最佳回答

Perl, 99 characters

@P=map{$/.substr$". __/  \ x99,$_,$W||=1+3*pop}0,(3,6)x pop;
chop$P[0-$W%2];print"    __"x($W/6),@P

www.un.org/Depts/DGACM/index_spanish.htm 同上,《:将一个特性改为-($W%2)改为0-$W%2<>。 (A. Rex)

<>Explanation:

就Width和H等高点而言,产出为2+2 *H线长,3W+1大体,产出中有许多重复。

为方便起见,我们请<代码> W 为3W+1, 即产出的性质。

顶线包括:<代码>__>, 重复W/2=$W/6/code>倍。

点号为<代码>/“, truncated to $ W natures。 第二行产出是一例,第二行的第一项特性应当是空间而不是<条码>。

奇数线包括重复模式/__>,编号为$ W natures。

我们构造一个插图: ”。 请注意,这一指示的开始是第二行的预期产出。 从立场3开始,这一行距是奇科的预期产出,从位置6开始,就连线而言。

出席<代码>>>>>>>的LIST论点 电话从0开始,然后是H重复(3,6)。 www.un.org/Depts/DGACM/index_french.htm 电话列出了从适当职位开始的、现为<代码>的子号。 W = 3

在印刷结果之前还有一项调整。 如果W是奇怪的,那么第二行($P[0])的外在性质需要改为chop。 即便如此,在底线上($P[-1])的外在性。

问题回答

Python 2.6 - 144 characters including newlines

如果允许投入分开,我可以节省大约20个。

C,R=map(int,raw_input().split())
print C/2*"    __"+"
 "+("__/  \"*99)[:3*C-C%2]
r=0
exec r+=3;print ("\__/  "*99)[r:r+3*C+1-r/6/R*~C%2]; *2*R

从指挥线获取意见的版本是4个更细化的内容:

import sys
C,R=map(int,sys.argv[1:])
print C/2*"    __"+"
 "+("__/  \"*99)[:3*C-C%2]
r=0
exec r+=3;print ("\__/  "*99)[r:r+3*C+1-r/6/R*~C%2]; *2*R

C89 (136 characters)

x;y;w;main(h){for(h=scanf("%d%d",&w,&h)*h+2;y++
<h;++x)putchar(x>w*3-(y==(w&1?2:h))?x=-1,10:
"/  \__"[--y?y-1|x?(x+y*3)%6:1:x%6<4?1:5]);}

Perl, 160 characters

$w=shift;for$h(-1..2*shift){push@a,join  ,(( \__ , /   )x($w+$h))[$h..$w+$h]}
$a[0]=~y#\/# #;$a[1]=~s/./ /;s/_*$//for@a;$a[$w%2||$#a]=~s/. *$//;print$_,$/for@a

根本不涉及任何冲突:仅仅填满具有特性的阵列,然后我们去掉看似的阵列。

平流体的主轴只有137个,而所有信贷都应寄给他。

$w=shift;$=$/;for$y(1..($h=2+2*shift)){print map+(split//, _ \__/   )
[$y-1?$y-2|$_?($_+$y%2*3)%6+2:1:$_%6<4],0..$w*3-!($w&1?$y-2:$y-$h)}

J, 143 characters

4(1!:2)~(10{a.)&,"1({.4 :0{:)".(1!:1)3
|:(18,(}:,32-+:@{:)3 3 8 1 1 10$~3*x){(,   &(0})"1,   &(0 1})"1)(,}."1)(}."1,}:"1)(3++:y)$"1 / ,:  _ 
)

在处理以其他语文假设的多功能插图和面向青少年的用户互动类型时,利用J感受非常宽松。 但我猜测这并不是too坏......

重新掌握思想(一旦你找到一种办法,以结构分寸的方式审视这个问题,法院就更容易与你合作),这里的主谋在124年(例如,时间比原来的更长):

4(1!:2)~({.4 :0{:)".(1!:1)3
(x}~   _1}(x=.-1-+:2|x){])((10{a.),(   ,,99# __/   ,:     __ ){~(i.>:3*x)+])"0]595 0,3 6$~+:y
)

C#, 216 characters

class B{static void Main(string[]a){int b=0,i=0,w=int.Parse(a[0])+1,z=2*w*(int.Parse(a[1])+1);for(;i<z;b=(i%w+i/w)%2)System.Console.Write("\/ "[i>w&(w%2>0?i<z-1:i!=2*w-1)?b>0?0:1:2]+(++i%w<1?"
":b>0?"__":"  "));}}

减少引信:

class B{
    static void Main(string[]a){
       int b=0,
           i=0,
           w=int.Parse(a[0])+1,
           z=2*w*(int.Parse(a[1])+1);

       for(;i<z;b=(i%w+i/w)%2)
           System.Console.Write(
             "\/ "[i>w&(w%2>0?i<z-1:i!=2*w-1)?b>0?0:1:2]
             +
             (++i%w<1?"
":b>0?"__":"  ")
           );
    }
}

我使用了以下方法:

input: 4 2
cols:  0 00 1 11 2 22 3 33 4 44     
row 0:" |  | |__| |  | |__| |"
    1:" |__|/|  ||__|/|  ||"
    2:"/|  ||__|/|  ||__|/|"
    3:"|__|/|  ||__|/|  ||"
    4:"/|  ||__|/|  ||__|/|"
    5:"|__|/|  ||__|/|  | |"
  1. Iterate from zero to (W+1)*(H*2+1). The *2 is because each comb is 2 lines tall, and +1 to account for the first line and end of lines.
  2. Render two "pieces" of a hexagon per iteration:
  3. Decide between " ", "", and "/" for the first part
  4. Decide between "__", "  ", and " " for the second part

如果你看一看一看一幅足够多的蜂蜜,这种模式就很明显。 一半的逻辑只是处理第一行、第二行末和最后一行的例外情况。

NewLisp: 257 chars

我确信这不是一个最佳解决办法:

(silent(define(i v)(println)(set v)(int(read-line))(i w)(set t(+(* 3 w)1)(第_/___/“(define)(p s e(b 0))(println(append(dup)(b)(dup)(b)(dup(s 6 l)w))(e))(p 0) 4(- t(% w 2)1))(otimes(n(h))(p 6)t 9(p))

减少引信:

(silent
  (define (i v)
          (println)
          (set v (int (read-line))))
  (i  w)
  (i  h)
  (set  t (+ (* 3 w) 1))
  (set  l "    __/  \__/  ")
  (define (p s e (b 0))
          (println (slice (append (dup " " b) (dup (s 6 l) w)) 0 e)))
  (p 0 t)
  (p 4 (- t (% w 2)) 1)
  (dotimes (n (- h 1))
    (p 6 t)
    (p 9 t))
  (p 6 t)
  (p 9 (- t(% (+ w 1)2))))

我确信,我可以以不同的方式书写,并节省两条线路和几条特性,例如,但时间已晚......

Golfscript, 88 characters

mobrule解决方案为基础。 把它缩小到这个时代,是许多工作。 新路线只是清晰的。

~:r;:c 3*):W 6/"    __"*n
[][0]r[3 6]*+{[" ""__/  \"99*+>W<]+.},;
c 2%-1 1if:r%)[-1<]+r%
n*

解释:

~:r;,:c              # read input into rows, columns
3 *):W               # store c*3+1 into W
6 /"    __"*n        # write out "    __" W/6 times, plus newline
[]                   # initialize output array
[0]r[3 6]*+          # create array [0] + [3,6] repeated r times
{                    # for every entry in the input array...
[" ""__/  \"99*+    #   create the magic string
>W<                  #   truncate it between [n:W], where n is the cur entry
]+                   #   store this line in the output array
.},;                 # repeat for every entry in array
                     # now to handle the extra cases:
c 2%-1 1if:r%        # reverse the array if c is odd, do nothing if it s even
)[-1<]               # take the last entry in the array, cut off the last char
+r%                  # put it back on the array, and un-reverse it
n*                   # now join the array with newlines


Here is my original entry at 118 characters:

入境时间过迟,但人数最少。 (我只用这些东西学习书。) 新路线是清晰的。

~:r;:c 2%:o;c 2/:b"    __"*n:e
{e" ""\"if"__/  \"b*o{"__"e{"":e}"/"if}{"":e}if n
"/""  \__/"b*o"  \"""if n}r*
"\__/  "b o+*

C89 - 261 necessary chars

所有白色空间都可以拆除。 我的解决办法是轮流担任理事会......

x,y,W,h,B[999],*a,*b,*c,*d;
main(w){
  for(scanf("%d%d",&h,&w);y<h;y++,*b++ = *c++ = 63)
    for(x=0,
        W=w*2+2-(h==1),
        a=B+y*W*3+y%2,
        b=a+W,
        c=b+W,
        d=c+W;x++<w;)

      *a++ = 60,
      *a++ = *d++ = 15,
      *b++ = *c++ = 63,
      *b++ = *c++ = 0,
      *d++ = 60;

  for(x=W;--x>=0;puts(""))
    for(y=0;y<h*3+1;putchar(B[x+y++*W]+32));
}

F#, 303 chars

let[|x;y|]=System.Console.ReadLine().Split([|   |])
let p=printf
let L s o e=p"%s"s;(for i in 1..int x do p"%s"(if i%2=1 then o else e));p"
"
if int x>1 then L" ""  "" __ ";L" ""__""/  \"
else L" ""__"""
for i in 1..int y-1 do(L"/""  \""__/";L"\""__/""  \")
L"/""  \""__/"
L"""\__/""  "

EDIT

现在,最后还有其他一些答案,我不想分享一个不太清晰的版本:

let [|sx;sy|] = System.Console.ReadLine().Split([|   |])
let x,y = int sx, int sy

let Line n start odd even =
    printf "%s" start
    for i in 1..n do
        printf "%s" (if i%2=1 then odd else even)
    printfn ""

// header
if x > 1 then
    Line x " "   "  "   " __ "
    Line x " "   "__"   "/  \"
else    
    Line x " "   "__"   "    "

// body
for i in 1..y-1 do
    Line x "/"    "  \"   "__/"
    Line x "\"   "__/"    "  \"

// footer
Line x "/"   "  \"    "__/"
Line x ""    "\__/"   "  "

C# 377 果园

Didn t want to disappoint anyone waiting for the "funny" C# answer. Unfortunately, it s not 250 lines though...;)


using System;
class P{
    static void Main(string[] a){
        int i,j,w=Int32.Parse(a[0]),h=Int32.Parse(a[1]);
        string n="
",e="",o=e,l="__",s=" ",r=s+s,b=@"",f="/";
        string[] t={r+r,l,b+l+f,r,l,f+r+b,e,f,b,s};
        for(i=0;i<w;)o+=t[i++%2];
        for(i=0;i<2*h;i++){
            o+=n+(i%2==0?i!=0?b:s:e);
            for(j=0;j<w;)
                o+=t[((j+++i)%2)+4];
            o+=i!=0?t[((w+i)%2)+6]:e;
        }
        o+=n;
        for(i=0;i<w;)o+=t[i++%2+2];
        Console.Write(o);
    }
}

Groovy, #375 chars

Sam logic logic ;

public class FunCode {
        public static void main(a) {
            int i,j,w=Integer.parseInt(a[0]),h=Integer.parseInt(a[1]);
            String n="
",e="",o=e,l="__",s=" ",r=s+s,b="\",f="/";
            def t=[r+r,l,b+l+f,r,l,f+r+b,e,f,b,s];
            for(i=0;i<w;)o+=t[i++%2];
            for(i=0;i<2*h;i++){
                o+=n+(i%2==0?i!=0?b:s:e);
                for(j=0;j<w;)
                    o+=t[((j+++i)%2)+4];
                o+=i!=0?t[((w+i)%2)+6]:e;
            }
            o+=n;
            for(i=0;i<w;)o+=t[i++%2+2]; println(o);
        }
    }

Lua, 227 characters

w,h,s=io.read("*n"),io.read("*n")*2+2," " for i=1,h do b=(i%2>0 and "/  \__" or "\__/  "):rep(w/2+1):sub(1,w*3+1) print(i==1 and b:gsub("[/\]",s) or i==2 and b:gsub("^\",s):gsub("/$",s) or i==h and b:gsub("\$",s) or b) end

<>亮度>208,从指挥线读出宽度和高度。

s,w,h=" ",... h=h*2+2 for i=1,h do b=(i%2>0 and "/  \__" or "\__/  "):rep(w/2+1):sub(1,w*3+1) print(i==1 and b:gsub("[/\]",s) or i==2 and b:gsub("^\",s):gsub("/$",s) or i==h and b:gsub("\$",s) or b) end




相关问题