“ 99瓶汽水”


55

挑战

编写一个将歌词输出到99瓶啤酒的程序,但是如果墙上的瓶子数是3的倍数,则输出“嘶嘶” ,不是“啤酒”,如果是5的倍数,则输出“嗡嗡声”,并且如果“ fizzbuzz”是3的倍数和5的倍数。如果墙上的瓶子数量不是3或5的倍数,则只需照常输出“啤酒”即可。

歌词

99 bottles of fizz on the wall, 99 bottles of fizz.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of fizz on the wall.

96 bottles of fizz on the wall, 96 bottles of fizz.
Take one down and pass it around, 95 bottles of buzz on the wall.

95 bottles of buzz on the wall, 95 bottles of buzz.
Take one down and pass it around, 94 bottles of beer on the wall.

....

3 bottles of fizz on the wall, 3 bottles of fizz.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of fizz on the wall.


这是,因此每种语言中最短的提交内容将获胜。


30
我有95瓶汽水。我带走一个。现在我有94瓶啤酒。逻辑。
Okx

2
我可以喝一瓶碳酸啤酒吗?
斯蒂芬

1
第三瓶之后应该换行吗?
Kritixi Lithos'5

2
两行之间是否需要换行?在Kritixi Lithos进行编辑之前,现在没有了。
dzaima

11
@Okx好吧,啤酒汽水,给你嗡嗡声……
Draco18s

Answers:


12

Python 2中263个 253 245字节

i=99
x=''
while i:x+=', %s on the wall.\n\n%s on the wall, %s.\n'%(('%d bottle%s of %s'%(i,'s'*(i>1),(i%3<1)*'fizz'+(i%5<1)*'buzz'or'beer'),)*3)+'GToa kteo  otnhee  dsotwonr ea nadn dp absusy  isto maer omuonrde'[i>1::2];i-=1
print x[35:]+x[:33]

在线尝试!


这太棒了!您能解释为什么插值字符串是最短的选择吗?
musicman523

2
例如,['ab','cd'][x]可以将其重写为'acbd'[x::2],只是为了节省一些字节
Rod

7

C(GCC),276 274字节

感谢尼尔节省了两个字节!

#define w" on the wall"
#define c(i)printf("%d bottle%s of %s",i,"s"+!~-i,i%3?i%5?"beer":"buzz":i%5?"fizz":"fizzbuzz"),printf(
i;f(){for(i=99;i;c((i?:99))w".\n\n"))c(i)w", "),c(i)".\n"),printf(--i?"Take one down and pass it around, ":"Go to the store and buy some more, ");}

谁不喜欢宏扩展中无与伦比的括号?

取消高尔夫:

#define c(i)                               \
    printf(                                \
        "%d bottle%s of %s",               \
        i,                   /* Number  */ \
        i-1 ? "s" : "",      /* Plural  */ \
        i % 3                /* FizzBuzz*/ \
            ? i % 5                        \
                ? "beer"                   \
                : "buzz"                   \
            : i % 5                        \
                ? "fizz"                   \
                : "fizzbuzz"               \
    )

i;
f() {
    for(i = 99; i; ) {
        c(i); printf(" on the wall, ");
        c(i); printf(".\n");
        printf(
            --i
                ? "Take one down and pass it around, "
                : "Go to the store and buy some more, "
        );

        // This has been stuffed into the for increment
        c((i?:99)); printf(" on the wall.\n\n");
    }
}

在Coliru上现场观看!

备用版本(276字节)

#define c(i)printf("%d bottle%s of %s",i,i-1?"s":"",i%3?i%5?"beer":"buzz":i%5?"fizz":"fizzbuzz"),printf(
i,*w=" on the wall";f(){for(i=99;i;c((i?:99))"%s.\n\n",w))c(i)"%s, ",w),c(i)".\n"),printf(--i?"Take one down and pass it around, ":"Go to the store and buy some more, ");}

这太酷了!我总是为使用字符串操作获得出色的C答案感到震惊。
musicman523

通过更改#define w" on the wall"为来节省一些字节*w=" on the wall"
MD XF

@MDXF mmh,我得到完全相同的字节数。我想念什么吗?
昆汀

我认为它们意味着您可以替换#define w*w=在其中保存字节。老实说,我对高尔夫球C并不那么熟悉,但是我猜想它构成w了一个隐式定义的全局char *。
musicman523

4
@ musicman523问题是#defined w是字符串文字,该文字会自动粘贴到相邻的字符串文字中。如果w是变量,则必须在中使用实际的字符串格式printf
昆丁

6

罗达(Röda)273字节

f{a=`bottle`f=` on the wall`g=`99 ${a}s of fizz`;[`$g$f, $g.
`];seq 98,1|{|b|d=`s`d=``if[b=1];c=``c=`fizz`if[b%3=0];c.=`buzz`if[b%5=0];c=`beer`if[c=``];e=`$b $a$d of $c`;[`Take one down and pass it around, $e$f.

$e$f, $e.
`]}_;[`Go to the store and buy some more, $g$f.`]}

在线尝试!

早上会打高尔夫球。


6

PHP,242字节

function f($k){return"$k bottle".(s[$k<2])." of ".([fizz][$k%3].[buzz][$k%5]?:beer);}$w=" on the wall";for($b=f($c=99);$c;)echo"$b$w, $b.
",--$c?"Take one down and pass it around":"Go to the store and buy some more",", ",$b=f($c?:99),"$w.

";

在线尝试!

PHP,244字节

for($e=s,$b=fizz,$c=99;$c;)echo strtr("301245, 30124.
6, 708295.

",[" bottle",$e," of ",$c,$b," on the wall",--$c?"Take one down and pass it around":"Go to the store and buy some more",$k=$c?:99,$e=s[2>$k],$b=[fizz][$k%3].[buzz][$k%5]?:beer]);

在线尝试!

使用功能strtr

PHP,245字节

$f=function($k)use(&$b){$b="$k bottle".(s[$k<2])." of ".([fizz][$k%3].[buzz][$k%5]?:beer);};for($w=" on the wall",$f($c=99);$c;)echo"$b$w, $b.
",--$c?"Take one down and pass it around":"Go to the store and buy some more",", {$f($c?:99)}$b$w.

";

在线尝试!

在字符串中使用匿名函数来获取sustring,具体取决于整数

展开式

$f=function($k)use(&$b){$b="$k bottle".(s[$k<2])." of ".([fizz][$k%3].[buzz][$k%5]?:beer);};
for($w=" on the wall",$f($c=99);$c;)
echo"$b$w, $b.
",--$c?"Take one down and pass it around":"Go to the store and buy some more"
,", {$f($c?:99)}$b$w.

";

1
如果我没有错误计数可以节省2个字节(250个字节总数): function x($n){return"$n bottle".($n-1?s:'')." of ".(($n%3?'':fizz).($n%5?'':buzz)?:beer);}$y=" on the wall";for($b=99;$b;){$c=x($b);echo"$c$y, $c.↵",--$b?"Take one down and pass it around":"Go to the store and buy some more",", ".x($b?:99)."$y.↵↵";}。:)
insertusername此处为'17

1
@insertusernamehere您误算了一些更改,可以节省2个字节。谢谢。而且你给我一点主意,使用use与匿名函数节省了1个字节在这个版本中的组合
约尔格Hülsermann

5

05AB1E151个 146 143字节

99LRv'¬ž“fizzÒÖ“#y35SÖÏJ‚˜1(è©y“ƒ¶€µ„‹€ƒî倕…¡, ÿÏꀂ ÿ€‰€€íÒ.“ªõ®y“ÿÏꀂ ÿ€‰€€íÒ, “D#4£ðýs…ÿÿ.}‚‚˜'Ïê'±¥:`)¦¦¬#7£ðý¨“‚œ€„€€ƒï€ƒ‚¥€ä€£, ÿ.“ª)˜»

在线尝试!


4

SOGL136个 135 134 133 131 字节

Ƨ, o▓k
"πFT+╔¡‘oW
³³q"'bμ⁸‘oH? so}5\;3\«+"ΞQv↑χāσκN⌡κYT¡‘_,S─‘oθwoX▓
MH∫}¹±{▓WkƧ.¶oH¡"sΗ─χpēGķ¶¾3Ζ^9f.⅟▒E┌Fρ_╬a→‘KΘw⁽oXH‽M}HkW">⁸‘p

首先,第三个功能:

                                    ▓  name this "▓" (example input: 15)                          [15]
³³                                     Create 4 extra copies of the top thing (number of things)  [15, 15, 15, 15, 15]
  q                                    output without popping one of them                         [15, 15, 15, 15, 15]
   "...‘o                              output " bottle"                                           [15, 15, 15, 15, 15]
         H?   }                        if pop-1 [isn't 0]                                         [15, 15, 15, 15]
            so                           output "s"                                               [15, 15, 15, 15]
               5\                      push if POP divides by 5                                   [15, 15, 15, 1]
                 ;                     swap [if divides & another number copy]                    [15, 15, 1, 15]
                  3\«                  push if POP divides by 3, multiplied by 2                  [15, 15, 1, 2]
                     +                 add those together                                         [15, 15, 3]
                      "...‘            push "buzz fizz fizzbuzz beer"                             [15, 15, 3, "buzz fizz fizzbuzz beer"]
                           ...‘o       output " of " (done over here to save a byte for a quote)  [15, 15, 3, "buzz fizz fizzbuzz beer"]
                                θ      split ["buzz fizz fizzbuzz beer"] on spaces                [15, 15, 3, ["buzz","fizz","fizzbuzz","beer"]]
                                 w     get the index (1-indexed, wrapping)                        [15, 15, ["buzz","fizz","fizzbuzz","beer"], "fizzbuzz"]
                                  o    output that string                                         [15, 15, ["buzz","fizz","fizzbuzz","beer"]]
                                   X   pop the array off of the stack                             [15, 15]

第一个功能:

Ƨ, o▓k
     k  name this "function" "k"
Ƨ, o    output ", "
    ▓   execute the "bottleify" function

第二个功能:

"πFT+╔¡‘oW
         W  call this "W"
"πFT+╔¡‘    push " on the wall"
        o   output it

主要部分:

MH∫}                                     repeat 99 times, each time pushing index
    ¹                                    wrap in an array
     ±                                   reverse it
      {                                  iterate over it
       ▓                                 execute that function
        W                                execute that function
         k                               execute that function
          Ƨ.¶o                           output ".\n"
              H¡                         push if POP-1 isn't 0 (aka 1 if pop <> 1, 0 if pop == 1)
                "...‘                    push "Stake one down and pass it aroundSgo to the store and buy some more"
                     K                   push the first letter of that string
                      Θ                  split ["take one down and pass it aroundSgo to the store and buy some more" with "S"]
                       w                 gets the xth (1-indexed, wrapping) item of that array
                        ⁽o               uppercase the 1st letter and output
                          X              pop the array off
                           H‽            if pop-1 [isn't 0]
                             M           push 100
                              }          ENDIF
                               H         decrease POP
                                k        execute that function
                                 W       execute that function
                                  ">⁸‘p  output ".\n\n"

由于O将换行符放在换行符前后的错误而丢失了几个字节(不知何故,它可以返回到V0.9(这是V0.11代码))


4

Java中,344个 340 339字节

(打高尔夫球后的-4字节;清除杂散的-1字节)

interface X{static void main(String[]a){for(int i=99;i>0;System.out.printf("%s on the wall, %s.%n%s, %s on the wall.%n%n",b(i),b(i--),i<1?"Go to the store and buy some more":"Take one down and pass it around",b(i<1?99:i)));}static String b(int i){return i+" bottle"+(i>1?"s":"")+" of "+(i%3<1?"fizz":"")+(i%5<1?"buzz":i%3<1?"":"beer");}}

略微倾斜(使用1空间缩进以消除水平滚动):

interface X {
 static void main(String[]a){
  for(int i=99;i>0;System.out.printf("%s on the wall, %s.%n%s, %s on the wall.%n%n",
   b(i),b(i--),
   i<1?"Go to the store and buy some more":"Take one down and pass it around",
   b(i<1?99:i)));
 }
 static String b(int i){
  return i+" bottle"+(i>1?"s":"")+" of "+(i%3<1?"fizz":"")+(i%5<1?"buzz":i%3<1?"":"beer");
 }
}

4

JavaScript(ES6),316 309字节

这是一个完整的程序,而不是一个功能。没什么很有创意的,只是天真的方法(因此字节数!)。我使用console.log()而不是alert()因为许多浏览器对可以显示的字符数有限制alert()。请注意,所有空格和换行符都是必需的。

a="";for(i=99;i>0;i--){b=j=>"bottle"+(j>1?"s":"");d=a=>(a%3?"":"fizz")+(a%5?"":"buzz")||"beer");w=" on the wall";o=" of ";a+=`${i+" "+b(i)+o+d(i)+w+", "+i+" "+b(i)+o+d(i)}.
${i>1?"Take one down and pass it around, ":"Go to the store and buy some more, "}${(y=i-1?i-1:99)+" "+b(y)+o+d(y)+w}.

`;}console.log(a)

松散

let accumulator = "";
for(let i = 99; i>0; i--){
    let bottleString = j => "bottle"+(j>1?"s":""),
    drink = a =>(a%3?"":"fizz")+(a%5?"":"buzz")||"beer",
    wallString = " on the wall",
    of=" of ";
    accumulator += `${i+" "+bottleString(i)+of+drink(i)+wallString+", "+i+" "+bottleString(i)+of+drink(i)}.
${i>1?"Take one down and pass it around, ":"Go to the store and buy some more, "}${(y=i-1?i-1:99)+" "+bottleString(y)+of+drink(y)+wallString}.

`;
}

console.log(accumulator);

这是代码段:

a="";for(i=99;i>0;i--){b=j=>"bottle"+(j>1?"s":"");d=a=>(a%3?"":"fizz")+(a%5?"":"buzz")||"beer";w=" on the wall";o=" of ";a+=`${i+" "+b(i)+o+d(i)+w+", "+i+" "+b(i)+o+d(i)}.
${i>1?"Take one down and pass it around, ":"Go to the store and buy some more, "}${(y=i-1?i-1:99)+" "+b(y)+o+d(y)+w}.

`;}console.log(a)

顺便说一句,有了这个答案,我已经在赢得了铜牌!从来没有想过我会做到这一点(尽管这不是一个很大的成就。)!


您的d函数不需要任何()s,因为它?:是右关联的,但实际上您可以使用来保存更多字节d=a=>(a%3?"":"fizz")+(a%5?"":"buzz")||"beer"
尼尔

3

视网膜,230字节


99$*_
_\B
Take one down and pass it around, $.'#.¶¶$.'#, $.'.¶
^
99#, 99.¶
_
Go to the store and buy some more, 99#.
#
 on the wall
1\b|(\d+)
$& bottle$#1$*s of $&$*_
\b(_{15})+\b
fizzbuzz
\b(_{5})+\b
buzz
\b(___)+\b
fizz
_+
beer

在线尝试!说明:


99$*_

插入99 _秒。

_\B
Take one down and pass it around, $.'#.¶¶$.'#, $.'.¶

将除最后一个以外的所有内容更改_为字符串Take one down and pass it around, $.'#.¶¶$.'#, $.'.¶,其中为换行符,$.'为剩余下划线的计数。有效地从98倒数到1。

^
99#, 99.¶

以“紧凑”格式添加第一节经文的第一行。

_
Go to the store and buy some more, 99#.

添加最后一节的第二行。为什么我需要跳篮球才能使用_我不知道的东西,但是$似乎匹配两次,所以我不能使用它。去搞清楚。

#
 on the wall

替换在诗句中出现几次的字符串。

1\b|(\d+)
$& bottle$#1$*s of $&$*_

这将与经文中的整数匹配,并在适当的瓶子上加上后缀,然后再次扩展为一元,以准备选择饮料。(我99这样在s 上保存了1个字节。)

\b(_{15})+\b
fizzbuzz
\b(_{5})+\b
buzz
\b(___)+\b
fizz
_+
beer

用适当的饮料代替精确的倍数。


2

SED468个 459 456字节

s:^:99 bottles of fizz on the wall, 99 bottles of fizz.:
p
s:99:I8:g
s:fizz:XYYZ:g
x
s:^:Take one down and pass it around, I8 bottles of XYYZ on the wall.\n:
G
x
:
g
s:XXX:fizz:g
s:Y{5}:buzz:g
s:\bX*Y*Z:beer:g
s:[XYZ]::g
y:ABCDEFGHI:123456789:
s:\b0::g
/ 1 /bq
p
x
s:^::
tc
:c
s:(\S)0:\1@:g
Td
y:ABCDEFGHI:0ABCDEFGH:
:d
y:123456789@:0123456789:
s:(XXX)*(Y{5})*(Y*Z):XY\3:g
x
b
:q
s:es:e:g
aGo to the store and buy some more, 99 bottles of fizz on the wall.

在线尝试!

需要-r标志。

说明

保持空间持有两个重复线的图案,用表示为数字[A-I][0-9](用于十位和个位数分别)和种类表示为饮料X*Y*Z,其中X保持的轨道-N mod 3,和Y-N mod 5

在每次后续迭代中,数字都会减少,并且Xs和Ys会更新。然后将保留空间复制到模式空间,将其转换成歌曲的行并进行打印。


2

C,349个 345 344字节

#define b(x)x^1?" bottles":" bottle"
#define g i-1?"Take one down and pass it around":"Go to the store and buy some more"
*w=" on the wall";*s(x){return x?x%15?x%5?x%3?"beer":"fizz":"buzz":"fizzbuzz":"fizz";}i=100;main(){while(--i){printf("%d%s of %s%s, %d%s of %s.\n%s, %d%s of %s%s.\n",i,b(i),s(i),w,i,b(i),s(i),g,i-1?i-1:99,b(i-1),s(i-1),w);}}

好吧,你去。那花了一个小时。

在线尝试!


2

使用Javascript(ES6),236个 234 233 232字节

for(i=99;i;console.log(z()+`, ${z(_)}.
${--i?'Take one down and pass it around':'Go to the store and buy some more'}, ${z()}.

`))z=(o=' on the wall',j=i||99)=>j+` bottle${j>1?'s':_} of `+((j%3?_:'fizz')+(j%5?_='':'buzz')||'beer')+o

演示版

// replace console.log to avoid 50-log limit in snippets:
console.log=_=>document.write(`<pre>${_}</pre>`)

for(i=99;i;console.log(z()+`, ${z(_)}.
${--i?'Take one down and pass it around':'Go to the store and buy some more'}, ${z()}.

`))z=(o=' on the wall',j=i||99)=>j+` bottle${j>1?'s':_} of `+((j%3?_:'fizz')+(j%5?_='':'buzz')||'beer')+o

不打高尔夫球

i = 99  // start counter at 99

z = (   // define function z which takes arguments with defaults:
   o = ' on the wall', // o = defaults to ' on the wall'
   j = i || 99         // j = defaults to value of counter i - or 99 when i == 0
) => 
    j +                 // our current j counter
    ' bottle' +
    (j > 1 ? 's' : _) + // choose 's' when we have more than 1 bottle, or blank _
    (
        (j % 3 ? _ : 'fizz') +      // if j % 3 is 0, add 'fizz', otherwise blank _
        (j % 5 ? _ = '' : 'buzz')   // if j % 5 is 0, add 'buzz', otherwise blank _
                                    // _ gets defined here since it's the first place it's used
            ||                      // if no fizz or buzz, result is a falsey empty string
        'beer'                      // replace falsey value with 'beer'
    ) +
    o                               // append o

while (i) {         // while counter is non-zero
    console.log(    // output string:
        z() +       // call z without o argument
        ', ' +
        z(_) +      // call z with blank _ for o to block ' on the wall' here
        '.\n' +
        ( --i       // decrement i, if still non-zero:
            ? 'Take one down and pass it around'
                    // otherwise:
            : 'Go to the store and buy some more'
        ) + 
        ', ' +
        z() +       // another call to z without o
        '.\n\n'
    )
}

1

红宝石,261字节

99.downto(1){|i|w=' on the wall'
f=->x{a='';x%3<1&&a+='fizz';x%5<1&&a+='buzz';a<?a&&a='beer';"%d bottle%s of %s"%[x,x<2?'':?s,a]}
puts [f[i]+w,f[i]+?.+$/+(i<2?'Take one down and pass it around':'Go to the store and buy some more'),f[i<2?99:i-1]+w+?.+$/*2]*', '}

在线尝试!


1

shortC314个 312字节

Db(x)x^1?" bottles":" bottle"
Dg i-1?"Take one down and pass it around":"Go to the store and buy some more"
*w=" on the wall";*s(x){Tx?x%15?x%5?x%3?"beer":"fizz":"buzz":"fizzbuzz":"fizz";}i=100;AW--i){R"%d%s of %s%s, %d%s of %s.\n%s, %d%s of %s%s.\n",i,b(i),s(i),w,i,b(i),s(i),g,i-1?i-1:99,b(i-1),s(i-1),w

抱歉,没有任何解释,但我完全忘记了它是如何工作的。


您应该考虑在shortC中添加另一个答案,答案遵循答案的逻辑,以查看其效果。此外,看起来在两个答案中,您仅对g使用一次宏,您应该可以内联它并保存一些字节
musicman523

您可以发布扩展版本吗?
CalculatorFeline

@CalculatorFeline 如果您要的是这里,等效的C代码已经在这里
musicman523

1

木炭307个 297字节

A”|‽2?{:×G↗”¦αA“6«eMηOU¶¿”¦ζA“9“e▷·gqε-g}”¦βA“9B{⦃⁺Bφ=;λO”¦ωAfizz¦φAbuzz¦γAbeer¦ηA”↶C▶▶d℅d¬r·US\λTθNevT◧→GM⁸ω┦τA“M↧k↓⁺*f÷,ψZ¢▶\¿|P“№κ×υpξXoW”¦σA.¶πF⮌…¹¦¹⁰⁰«A⎇⁻ι¹αζθ¿∧¬﹪鳬﹪ι⁵A⁺φγ﹪ι³Aφ﹪ι⁵AγεAηε⁺⁺⁺⁺⁺⁺⁺IιθεβIιθεπ¿⁻ι¹A⁻ι¹λA⁹⁹λA⎇⁻λ¹αζθ¿∧¬﹪볬﹪λ⁵A⁺φγ﹪λ³Aφ﹪λ⁵AγεAηε¿⁻ι¹AτδAσδ⁺⁺⁺⁺δλθεω

在线尝试!

我们可以!链接到详细版本,我敢肯定,这可以打很多。


不幸的是,您忘了实际链接到详细版本,但s的运行看起来可疑...
Neil

1

tcl,298

proc B i {set x " bottle[expr $i>1?"s":""] of [expr $i%3?$i%5?"beer":"":"fizz"][expr $i%5?"":"buzz"]"}
set i 99
time {puts "$i[B $i][set w " on the wall"], $i[B $i].
Take one down and pass it around, [incr i -1][B $i]$w."} 98
puts "1[B $i]$w, 1[B $i].
Go to the store and buy some more, 99[B 9]$w."

演示

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.