给我烤一个蛋糕!


15

这是一块蛋糕:

_========_
|        |
+________+
|        |
+________+
|        |
+________+

它是8宽,3高和1深。

您必须编写一个程序,通过3个输入来制作蛋糕。第一个输入控制中间有多少个下划线=,顶部是s。这是第一个宽度为10而不是8的蛋糕:

_==========_
|          |
+__________+
|          |
+__________+
|          |
+__________+

第二个输入控制蛋糕的高度。这是第二个蛋糕,高度为4而不是3:

_==========_
|          |
+__________+
|          |
+__________+
|          |
+__________+
|          |
+__________+

注意各层的重复。

第三个输入控制它的深度。那就是| |要包含在顶部的个数。这是深度为2而不是1的第三个蛋糕

_==========_
|          |
|          |
+__________+
|          |
+__________+
|          |
+__________+
|          |
+__________+

您可以打印尾随空格。测试用例:

输入:333

输出:

_===_
|   |
|   |
|   |
+___+
|   |
+___+
|   |
+___+

(我希望我永远不会得到这个蛋糕)

输入:321

输出:

_===_
|   |
+___+
|   |
+___+

输入:555

输出:

_=====_
|     |
|     |
|     |
|     |
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+

输入将始终为正整数吗?
尼克·克利福德

@NickClifford是的。

是否允许尾随换行符?
毛茸茸的


@Shaggy我以为是这样,默认情况下在Meta上是。
程序员

Answers:


9

V25,20字节

2é_Àé=ÙÒ|èÙÒ+È_ÀäkÀÄ

在线尝试!

十六进制转储:

00000000: 32e9 5fc0 e93d d9d2 7ce8 d9d2 2bc8 5fc0  2._..=..|...+._.
00000010: e46b c0c4                                .k..

感谢@ nmjmcman101保存了三个字节,并让我想起了一个旧的运算符,它又保存了两个字节。

说明:

abc是三个参数。

2é_                   " Insert two '_' characters
   Àé=                " Insert 'a' '=' characters between them
      Ù               " Duplicate this line
       Ò|             " Replace this whole line with '|'s
         è            " *Hollow* this line (replace all the middle characters with spaces)
          Ù           " Duplicate this line
           Ò+         " Replace this whole line with '+'s
             È_       " *Hollow* this line again, but use '_' instead of spaces
               Àäk    " Make 'b' copies of this line and the line above it
                  ÀÄ  " Make 'c' copies of this line

我想你可以替换Ò r|$.使用Ò|è的一些字节。在线尝试!
nmjcman101

@ nmjcman101噢,老兄,我完全忘记了它的è存在。我不得不绞尽脑汁记住它的作用。但这真的很聪明!实际上,使用大写形式È<char>也可以在其他一些地方节省更多的字节。谢谢你的提醒!:)
DJMcMayhem

4

木炭34 26字节

Nγ←×γ_↑+↑N_×γ=‖BOγF⁻N¹C⁰±²

在线尝试!链接是详细版本的代码。以宽度,深度,高度的顺序获取参数。说明:

Nγ          Input the width.
←×γ_        Print a set of _s that go at the bottom of each layer.
↑+          Print one of the +s that go on the left.
↑N          Input the depth and print that many left |s.
_           Print the top left _.
×γ=         Print the =s along the top.
‖BOγ        Copy the left column to the right.
F           Repeat:
 ⁻ ¹         One time fewer than:
  N           Input of the height:
    C⁰±²        Copy the whole cake up 2 characters.

2

Mathematica,167个字节

c=Column;r=Row;t=Table;f=Flatten;c[c/@{r/@f[{{{"_",r@t["=",#],"_"}},t[{"|",r@t[" ",#],"|"},#3-1]},1],c/@f[{t[{r@{"|",r@t[" ",#],"|"},r@{"+",r@t["_",#],"+"}},#2]},1]}]&




1

果冻30 29 字节

-1字节从加法转换为XOR,以在外部列和内部列之间转换,从而允许5个字符的查找而不是具有两个_条目。

ṬṚ;⁹RḤṬḤ¤Wµ^9ẋ⁵;@;µZị“_+= |”Y

完整的程序采取三个程序参数depthheightwidth和打印蛋糕。

在线尝试!

怎么样?

ṬṚ;⁹RḤṬḤ¤Wµ^9ẋ⁵;@;µZị“_+= |”Y - Main link: depth, height (width is a program argument)
Ṭ                             - untruth   [0,0,0,...1] such that the length is the depth
 Ṛ                            - reverse   [1,0,0,...0]
        ¤                     - nilad followed by link(s) as a nilad:
   ⁹                          -   link's right argument, height
    R                         -   range   [1,2,3,...,height]
     Ḥ                        -   double  [2,4,6,...,2*height]
      Ṭ                       -   untruth [0,1,0,1,0,1,...,0,1] (length double height)
       Ḥ                      -   double  [0,2,0,2,0,2,...,0,2]
  ;                           - concatenate  [1,0,0,...,0,0,2,0,2,0,2,...,0,2]
                              -     ...this is the form of a column of cake!
         W                    - wrap in a list
          µ                   - monadic chain separation, call that c
           ^9                 - bitwise XOR c with 9 [8,9,9,...,9,9,11,9,11,9,11,...,9,11]
              ⁵               - program's 3rd argument, width
             ẋ                - repeat the augmented c width times
               ;@             - concatenate with a copy of c
                 ;            - concatenate a copy of c
                  µ           - monadic chain separation call that sideways cake
                   Z          - transpose the sideways cake to put it the right way up
                     “_+= |”  - literal ['_','+','=',' ','|'] (cake decoration)
                    ị         - index into (1 based and modular, so 8,9, and 11 are, mod 5,
                                            3, 4, and 1 yielding '=', ' ', and '_')
                            Y - join with new lines
                              - implicit print



1

Java 7中169个 164 158字节

String f(int...a){String s="_",t="|",u="+";for(;a[0]-->0;s+="=",t+=" ")u+="_";s=s+"_";t="\n"+t+"|";u=t+"\n"+u+"+";for(;a[2]-->1;)s+=t;for(;a[1]-->0;)s+=u;return s;}

在线尝试!

取消高尔夫:

String f(int...a)                    // saves two bytes over int a, int b, int c
{
    String s="_", t="|", u="+";      // set up the start of each row

    for(; a[0]-->0; s+="=", t+=" ")  // Uses the goes-to operator to fill the row
        u+="_";                      

    s += "_\n";                      // adds the end of each row
    t += "|\n";              
    u = t + u + "+\n";               // and combining t into u

    for(; a[2]-->1; )                // add the top of the cake
        s += t;

    for(; a[1]-->0; )                // add the rest of the cake
        s += u;

    return s;
}

你可以将节省1个字节u=t+u+"+\n" inside the for-loop: 为(U = T + U +“+ \ N;一[2] - > 1)S + = T;`可是不错的答案,+1从我
凯文Cruijssen

1

05AB1E33 31字节

'_'=¹×«Ć,'|¹úRĆ³G=}²F='+'_¹×«Ć,

在线尝试!

说明

'_'=¹×«Ć,'|¹úRĆ³G=}²F='+'_¹×«Ć,   Main link. Args: w h d
'_                                Push literal '_'
  '=¹×                            Push '=' w times
      «Ć,                         Concat, enclose and print
         '|                       Push literal '|'
           ¹ú                     Pad with w spaces in front
             RĆ                   Reverse and ecnlose
               ³G }               d - 1 times do:
                 =                Print without consuming
                   ²F             h times do:
                     =            Print without consuming
                      '+          Push literal '+'
                        '_¹×      Push '_' w times
                            «Ć,   Concat, enclose and print

1

Windows批处理,211个180 163字节

Golfed共48字节感谢@Neil!

@for /l %%p in (1,1,%1)do @call set w= %%w%%
@echo _%w: ==%_
@for /l %%p in (2,1,%3)do @echo ^|%w%^|
@for /l %%p in (1,1,%2)do @echo ^|%w%^|&echo +%w: =_%+
@set w=

1
1. @在每行及之后使用的时间do略短于@echo off。2. @call set w=%%w%%_避免setlocal enabledelayedexpansion。3.使用@for /l %%p in (2,1,%2)。4.恕我直言,您应该添加@set w=代码,这样代码才能多次运行。
尼尔

我对最后一部分感到困惑,如果我不加@set w=代码,那么代码将无法再使用一次?
stevefestl

1
1.您@for /l %%p in (1,1,%1)do似乎失踪了。2.您的蛋糕似乎错了高度,至少在我尝试它时似乎与测试用例不匹配。3.如果您在同一命令会话中两次运行脚本,则蛋糕会越来越大。
尼尔

@Neil都是固定的:)
stevefestl

1
大!我注意到的最后几件事:1 . @之后不需要&。2如果将第二个循环更改为(2,1,%3)第三个循环,(1,1,%2)那么我认为可以删除该echo +%w%+行。3.如果我计算正确,那么我认为您应该w用空格而不是_s 填充,因为这将导致更少的替换。(我会用@call set w= %%w%%这个空间,否则很难发现!)
Neil

1

Haskell,87个字节

f w t d=["_=| +_\n"!!j|i<-0:([2..d]>>[2])++([1..t]>>[2,4]),j<-i:([1..w]>>[i+1])++[i,6]]

1
很好,把大多数非高尔夫语言都抛在了后面。中缀声明(w#t)d= ...保存一个字节。
Laikoni '17

1

SOGL V0.12,25字节

e =*¼_Oe↕¼|.⌡Qe╔*¼+.H«{Q;

在这里尝试!

期望输入为宽度,深度,然后为高度。


是否可以重新排序输入?它们似乎按规定的顺序指定。
递归

1
@recursive通常是允许的,问题不会在任何地方强制排序,并且投票数第二高的答案会对输入进行重新排序,并且可能已被OP看到,所以我认为这很好。
dzaima

1

Python 2,124 122 120 105 92字节

w,t,d=input()
a="\n|"+w*" "+"|"
print"_"+w*"="+"_"+(d-1)*a+t*(a+"\n+"+w*"_"+"+")

通过使用STDIN而不是程序参数来获得-15个字节

切换至Python 2来获得-13个字节(用于input()整数和print语句)

Caird Coinheringaahing的-12个字节

在线尝试!

Python 3中,124个 122 120 105字节

w,t,d=[int(input())for n in(1,2,3)]
a="\n|"+w*" "+"|"
print("_"+w*"="+"_"+(d-1)*a+t*(a+"\n+"+w*"_"+"+"))

在线尝试!

如果不需要完整的程序:

Python 3,87 84字节

lambda w,t,d:"_"+w*"="+"_"+(d-1)*("\n|"+w*" "+"|")+t*("\n|"+w*" "+"|\n+"+w*"_"+"+")

在线尝试!



@cairdcoinheringaahing 78个字节
乔纳森·弗雷希

0

使用Javascript(ES6),161个 157字节

f=(w,h,d)=>{r=x=>x.repeat(w);_='_';m='+'+r(_)+'+';b='|'+r(' ')+'|';c=[_+r('=')+_];for(i=d-1;i--;)
c.push(b);for(i=h;i--;)
c.push(b+'\n'+m);return c.join`\n`}

console.log(f(8,3,1));




0

JavaScript / ES6,90个字节

我只是写了一个粗略的解决方案,碰巧以高达56个字节的优势击败了现有的JS答案。然后我打了11个字节。

(w,h,d,g=a=>a+a[1][r='repeat'](w)+a[0]+`
`)=>g('_=')+(l=g('| '))[r](d-1)+(l+g('+_'))[r](h)

这是一个演示。

var F = (w,h,d,f=a=>a+a[1][r='repeat'](w)+a[0]+`
`)=>f('_=')+(l=f('| '))[r](d-1)+(l+f('+_'))[r](h);

console.log(F(prompt('width') || 3, prompt('height') || 3, prompt('depth') || 3));
console.log(F.toString().length);


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.