99瓶啤酒[关闭]


65

在墙上重新创建“ 99瓶啤酒”。

所需的输出是这样的:

99 bottles of beer on the wall, 99 bottles of beer.
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 beer on the wall.

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

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

....

3 bottles of beer on the wall, 3 bottles of beer.
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 beer on the wall.

告诉我如何使用自己喜欢的编程语言解决此问题。最有创意的语言使用会获胜。


30
我认为,“最有创意的用途”对于SE网站来说太主观了。我们应该旨在使用客观标准,例如“最短代码”。
克里斯·杰斯特·杨

5
仍然是一个很好的问题
Chris Laplante

3
它应该无限运行吗?我以为是。另外,codegolf并不拥有99瓶啤酒,因此甚至没有理由讨论它。

1
@Chris-这将是该SE网站最困难的部分,因为整个过程都是基于创造力的,因为这是编程难题的一半乐趣。最具创意的解决方案并不总是最有效或最短的。
Nick Berardi

2
@尼克:我听到你了。我只是想知道如何根据自己的判断来确定解决方案的创造性?基于选民的?两者都不是特别可靠(无犯罪)。简洁是无可争辩的,例如,没有人可以质疑1字节堆栈溢出代码高尔夫解决方案的“优缺点”。
克里斯·杰斯特·杨

Answers:


70

Perl(410个字符)

已经一个专门的网站,以本次比赛:

它的Perl解决方案之一在创造力方面很难被击败,它写道:

[bottles.pl]

         $ a =
        “ cpuu
       \ bmft p
       \ bg cff
       \ bs“; $ b
       =“ po ui
       \ bf xbm
      \ bm“; $ c =”
      尤文
     \ bof epxo qb
   \ btt ju bspvoe“;
  $ a =〜s / \ n //; $ a =〜
  s / \ s + / / g; $ b =〜
  s / \ n //; $ b =〜
  s / \ s + / / g; $ c =〜
  s / \ n //; $ c =〜
  s / \ s + / / g; $ a =〜
  y / bz / az /; $ b =〜
  tr / bz / az /; $ c =〜
  tr / bz / az /; 对于(
  $ d = 100; $ d> 0; $ d-){
  打印“ $ d $ a $ b $ d”
  ;打印“ $ a,\ n $ c,”
  ;打印($ d-1);打印
  “ $ a $ b。\ n”;} $ x =
  “ cjc”;$ y =“ dobbz”;
  $ z =“ com”; print“ \ n”
  ;打印“-$ x \ @ $ y”。
   ;打印“ $ z \ n \ n”;

这是原始文件的链接。


1
不错的演示,但是与要求不符!
F. Hauri 2014年

“ rot1”密码,不是我所见过的最好的混淆方法。使用\b“删除”不需要的字符也有些令人失望。
primo 2014年

100

Brainf ***(1,509)

我认为我不但可以制造9个而不是1个啤酒瓶,而且可以在代码中仅使用7个不同的字符,从而胜过这个答案。

    +++           +++           +++           [>+           +++           ++>           +++           +++           <<- 
    ]>+           ++>           +++           >++           +++           +++           ++>           +++           +++ 
   +++>+         +++++         +++++         [>+++         >++++         >++++         <<<-]         >->>+         +>+++ 
   +++++         [>+++         +++++         ++++>         +++++         +++++         ++>++         +++++         +++++ 
   >++++         +++++         +++>+         +++++         +++++         +>+++         +++++         +++++         >++++
   +++++         ++++>         +++++         +++++         +++>+         +++++         +++++         ++>++         +++++ 
  ++++++>       +++++++       ++++++>       +++++++       +++++++       >++++++       +++++++       +>+++++       +++++++
  ++>++++       +++++++       +++>+++       +++++++       ++++>++       +++++++       +++++<<       <<<<<<<       <<<<<<<
  <-]>+>+       +>++++>       +++++>+       +++++>>       +>+++>+       +++>+++       +++>+++       ++++>>+       +>+++>+ 
 +++>+++++     >+++++++<     <<<<<<<<<     <<<<<<<<<     <<<<[>[<<     <.>.>>>>.     >>>>>.>>>     >>>>>>.>>     >>..<<<<<
<.<<<<<.>>>   >>>>>>>.<<<   <<<<<<<<<<<   <<<.>>>>>>>   >>>>>>>.<<<   <<<.<<<<<<<   <.>>>>>.>>.   .>>>>>>>>>.   <<<<<<<<<<<
<<<<<.>>>>>   >>>>>>>>>.<   .<<<<<<<<<<   <<<.>>>>>>>   >>>>>>>>>>>   .<<<<<<<<<.   <<.<<<<<<<.   >>>>>>>>>>>   >>>>>>>>>.<
<<<<<<<<<<<   <<<<.>>>>>>   >>..<<<<<<<   <<<<.<.<<<<   <.>.>>>>.>>   >>>.>>>>>>>   >>.>>>>..<<   <<<<.<<<<<.   >>>>>>>>>>.
<<<<<<<<<<<   <<<<<<.>>>>   >>>>>>>>>>.   <<<<<<.<<<<   <<<<.>>>>>.   >>..>>>>>>>   >>.<<<<<<<<   <<<<<<<.<.>   >>>>>>>>>>>
>>>>>>.<<<<   <<<<<<<<<<.   >>>>>>>.<<<   <.<<<<<<<.>   >>>>>>>>>>>   >>.<.<<<<<<   .<<<<<<<.>>   >>>>.>>>>>>   >>.>>>>>>.<
<<<<<<.<<<<   <<<<<<<<<.>   >>>.>>>>>>>   >>.<<<<<<<.   <<<<<<.>>>>   >>>>>>>>>>>   .<<<<<<<<<<   <.>>>>>>>>>   >>>>..<<<<<
<<<<<<<<<<<   <.>>>>>>>>>   >.>>>>>>>>.   <<<<<<<<<<<   <<<<<<<.>>>   >.>>>>>>>>>   >>>.<<.>>>>   >.<<<<<<.<<   <<<<<.<<<<<
.<.<<<<<.>-   .>>>>.>>>>>   .>>>>>>>>>.   >>>>..<<<<<   <.<<<<<.>>>   >>>>>>>.<<<   <<<<<<<<<<<   <<<.>>>>>>>   >>>>>>>.<<<
<<<.<<<<<<<   <.>>>>>.>>.   .>>>>>>>>>.   <<<<<<<<<<<   <<<<<.>>>>>   >>>>>>>>>.<   .<<<<<<<<<<   <<<.>>>>>>>   >>>>>>>>>>>
 .<< < <<<     <<< . <<.     <<< < <<<     .>> > >>>     >>> > >>>     >>> > >>>     .<< < <<<     <<< < <<<     <<< . >>> 
  >> >>>.       .<<< <<       <<<< <.       << . <<       -]+ +++       ++ + ++       << + ++       ++ + ++       +<->>-]

我确实必须承认,代码中有一个错误(也许您可以弄清楚如何为我修复它?)并且它不会打印出最后一句话,Go to the store and buy some more, 99 bottles of beer on the wall.但是除此之外,它的功能和其他任何功能一样用其他所有人似乎都喜欢使用的娘娘腔编程语言制作的程序。


9
那里只有9瓶。您还需要90个!
Joe Z.

请愿书:将此语言重命名为braindrunk
Cyoce

99

功能

我是前几天写的。:)(屏幕截图:开始结束

由于额外的行间距,在StackExchange中这看起来很难看,因此请考虑在浏览器的JavaScript控制台中运行以下代码来解决此问题: $('pre').css('line-height',1)

                                            ╓┬────╖
     ╔════╗  ┌───╖                          ╟┘99b ║
     ║ −1 ╟──┤ + ╟──┐                       ╙──┬──╜
     ╚════╝  ╘═╤═╝  ├──────────────────────────┴─────────────────────────────┐
     ╔════╗  ┌─┴─╖  │  ╔════════════════════════════════════════════════════╗│
     ║ 99 ╟──┤ ? ╟──┘  ║ 93438979891487426396059469986395555362079573844971 ║│
     ╚════╝  ╘═╤═╝     ║ 71377306928718494179034460561943201885027745835961 ║│
            ┌──┴───╖   ║ 98129935108241412387473531261660077880505710501626 ║│
    ╔════╗  │ 99bp ║   ║ 32694396343717333192558234646820019070451056711    ║│
    ║ 99 ║  ╘══╤═══╝   ╚══════════════════════════╤═════════════════════════╝│
    ╚═╤══╝   ┌─┴─╖                       ┌───╖  ┌─┴─╖  ╔═════════════════╗   │
   ┌──┴──╖   │ ‼ ╟───────────────────────┤ ‼ ╟──┤ ? ╟──╢ 445551776368547 ║   │
   │ 99b ║   ╘═╤═╝┌─────────────────────┐╘═╤═╝  ╘═╤═╝  ║ 925186328623383 ║   │
   ╘══╤══╝     │  │╔═══════════════════╗│  │      │    ║ 851314944882510 ║   │
      │        │  │║ 15177132563375318 ║│  │      │    ║ 812246570019017 ║   │
 ╔════════╗    │  │║ 07655616350359109 ║│  │      │    ║ 240477365113929 ║   │
 ║ 318287 ║    │  │║ 82597577171382437 ║│  │      │    ║ 659548419629671 ║   │
 ║ 023073 ║    │  │║ 18150105146396039 ║│  │      │    ║ 952755268258505 ║   │
 ║ 603558 ║    │  │║ 2022986808360992  ║│  │      │    ║ 759402210908648 ║   │
 ║ 743780 ║    │  │╚══════════╤════════╝│  │      │    ║ 737406010882693 ║   │
 ║ 068900 ║    │  │         ┌─┴─╖ ┌───╖ │  │      │    ║ 018745757193818 ║   │
 ║ 028319 ║    │  │         │ ‼ ╟─┤ ‼ ╟─┘  │      │    ║ 597439618635403 ║   │
 ║ 948400 ║    │  │         ╘═╤═╝ ╘═╤═╝    │      │    ║ 821854707881243 ║   │
 ║ 620075 ║    │  │         ┌─┴─╖   │    ┌─┴─╖    │    ║ 92049082452     ║   │
 ║ 955580 ║    │  └─────┬───┤ ‼ ╟────────┤ ‼ ║    │    ╚═════════════════╝   │
 ║ 347161 ║    │        │   ╘═══╝┌──────┐╘═╤═╝    └─────────────┐            │
 ║ 651333 ║    │   ╔═══╗│┌──────╖│╔════╗│ ╔╧═════════╗          │            │
 ║ 590970 ║    │   ║ 0 ║└┤ 99bp ╟┘║ −1 ║└┐║ 20971566 ║          ├────────────┘
 ║ 678045 ║    │   ╚══╤╝ ╘══════╝ ╚══╤═╝ │╚══════════╝          │
 ║ 336290 ║  ┌─┴─╖  ┌─┴─╖  ┌─────╖  ┌┴──╖├──────────────────────┘
 ║ 721824 ╟──┤ ‼ ╟──┤ ? ╟──┤ 99b ╟──┤ + ║│
 ╚════════╝  ╘═══╝  ╘═╤═╝  ╘═════╝  ╘═╤═╝│    ╓┬──────╖
                      └───────┬───────┘  │    ╟┘ 99bp ║
                              └──────────┘    ╙───┬───╜
 ┌────────────────────────────────────────────────┴──────────────┐
 │╔══════════════════════════════════════════╗╔═══════════╗      │
 │║ 8592134145756414358602136806465202028576 ║║ 232783950 ║      │
 │╚══════════════════════════════╤═══════════╝╚╤══════════╝      │
 │               ┌───╖  ╔═══╗  ┌─┴─╖  ┌───╖  ┌─┴─╖  ┌─────────╖  │
 └───────────────┤ = ╟──╢ 1 ║  │ ‼ ╟──┤ ‼ ╟──┤ ? ╟──┤ int→str ╟──┴┐
                 ╘═╤═╝  ╚═══╝  ╘═╤═╝  ╘═╤═╝  ╘═╤═╝  ╘═════════╝   │
          ╔═══╗  ┌─┴─╖         ┌─┴─╖    │      └──────────────────┘
          ║ 0 ╟──┤ ? ╟─────────┤ ‼ ╟──┐
          ╚═══╝  ╘═╤═╝         ╘═══╝  │
              ╔════╧╗╔════════════════╧════════════════════════════════╗
              ║ 115 ║║ 20338288213193790107412311132593873016630280224 ║
              ╚═════╝╚═════════════════════════════════════════════════╝

1
加一,这真棒
gyurisc 2011年

1
那门语言很棒,干得好!
停止转动逆时针

1
我在Esolang.org上看到了它,并喜欢它的外观。
ML 2015年

56

jQuery + FireBug控制台

$('code:first').text()

;)


2
什么像eval($($('code')[8]).text().replace(/print/g,'console.log'))<DEL>(亦称偷)</ DEL>?它会打印歌曲的整个歌词。:P
JiminP 2011年

2
也可以在Chrome的JavaScript控制台上使用。
Spoike 2011年

极其聪明。
2012年

1
@JiminP不幸的是,考虑到答案顺序如何随时间变化,这并不是一个稳定的选择器(实际上,它不再起作用)。
Muhd 2013年

2
只要问题未更改,@ Muhd仍然可以使用-第一个代码块就是问题中的那个。
鲍勃

24

HQ9 +(1个字符)

9

诚然,它不是图灵完整的语言,但这仍然很重要


13
为什么不使用HQ9 B?图灵完成了。
Mateen Ulhaq 2011年

1
@muntoo我不确定HQ9 + B是否已完成演奏。.命令B轮询键盘输入并将其评估为Brainfuck,因此它是带有调性Brainfuck解释器的HQ9 +。即HQ9 + B中的程序“ B”将轮询stdin直到获得!(或EOF)并将其解释为Brainfuck。
Sylwester 2014年

20

谁说C#参加了太多的仪式?不管是谁,他们从未如此正确。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _99Bottles
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintSong(99);
        }

        static void PrintSong(int bottleCount)
        {
            Func<int, string> sOrBlank = howMany => howMany > 1 ? "s" : "";

            PrintBottles(howManyBottles =>
            {
                Console.WriteLine("{0} bottle{1} of beer on the wall, {0} bottle{1} of beer.", howManyBottles, sOrBlank(howManyBottles));
                if (howManyBottles > 1)
                {
                    Console.WriteLine("Take one down and pass it around, {0} bottle{1} of beer on the wall.", --howManyBottles, sOrBlank(howManyBottles));
                }
                else
                {
                    Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the wall.", --howManyBottles);
                }
            },
            bottleCount);
        }

        static void PrintBottles(Action<int> printBottles, int count)
        {
            printBottles(count);

            if (count > 1)
            {
                PrintBottles(printBottles, --count);
            }
         }
    }
}

11
+1成为Perl海洋中的另一只C型牺牲羔羊。
Glenn Nelson

这可以大大减少。
Nellius

2
@Nellius你甚至什么ilivewithian他的代码之前说的吗?
Daniel Standage

1
@Daniel是的,这意味着他认为代码中的所有内容都是必需的。如果只是开个玩笑,就不应该被这么大地投票。它不短,创新,高效,甚至不高明。
Nellius 2011年

@Nellius这并不是认真的竞争者,在任何方面都没有这个好的代码。我敢肯定,它可以减少,提高效率并大幅度改善。
罗伯·怀特

18

C

该程序使用预处理器将完整的歌曲文本生成为单个字符串。实际的C代码仅输出由此构造的字符串。调用strings生成的可执行文件将在可执行文件中显示完整的歌曲文本。

#define BOTTLES(n) n " bottles of beer"
#define BOTTLE "1 bottle of beer"
#define OTW " on the wall, "
#define TAKE "Take one down, pass it around, "
#define BUY "Go to the store and buy some more, "
#define STOP "."
#define NL "\n"

#define LINE1(n) BOTTLES(n) OTW BOTTLES(n) STOP NL
#define LINE1A BOTTLE OTW BOTTLE STOP NL
#define LINE2(n) TAKE BOTTLES(n) STOP NL
#define LINE2A TAKE BOTTLE STOP NL
#define LINEX BUY BOTTLES("99") NL

#define MIDDLEPART(n) LINE2(n) NL LINE1(n)
#define MIDDLELAST LINE2A NL LINE1A

#define EIGHT_TO_TWO(S, M) M(S "8") M(S "7") M(S "6") M(S "5") M(S "4") M(S "3") M(S "2")
#define EIGHT_TO_ONE(S, M) EIGHT_TO_TWO(S, M) M(S "1")
#define EIGHT_TO_TWO_AGAIN(S, M) M(S "8") M(S "7") M(S "6") M(S "5") M(S "4") M(S "3") M(S "2")
#define EIGHT_TO_ONE_AGAIN(S, M) EIGHT_TO_TWO_AGAIN(S, M) M(S "1")
#define NINE_TO_TWO(S, M) M(S "9") EIGHT_TO_TWO(S, M)
#define EIGHT_TO_ZERO(S, M) EIGHT_TO_ONE(S, M) M(S "0")
#define NINE_TO_ZERO(S, M) M(S "9") EIGHT_TO_ZERO(S, M)

#define NINETIES EIGHT_TO_ZERO("9", MIDDLEPART)
#define NTIES(n) NINE_TO_ZERO(n, MIDDLEPART)
#define EIGHTIES_TO_TENS EIGHT_TO_ONE_AGAIN("", NTIES)
#define NAUGHTIES NINE_TO_TWO("", MIDDLEPART)

#define SONG LINE1("99") NINETIES EIGHTIES_TO_TENS NAUGHTIES MIDDLELAST LINEX

#include <stdio.h>

int main()
{
  puts(SONG);
  return 0;
}

1
这些可能是最有效,最快的解决方案。
TomPažourek2014年

1
@tomp时间高效,可执行文件大小效率非常低:)
Seequ 2014年

14

C#(312个 310 304字符)

class P{static void Main(){string b=" bottle",w=" on the wall",o=" of beer",p=".\n",s="s";for(int i=99;i>0;i--)System.Console.Write(i+b+(i>1?s:"")+o+w+", "+i+b+(i>1?s:"")+o+p+(i>1?"Take one down and pass it around, "+(i-1)+b+(i-1>1?s:"")+o+w+p+"\n":"Go to the store and buy some more, "+99+b+s+o+w+p));}}

1
我简直不敢相信你比我短。我印象深刻 我将不得不研究一组更好的选择
jcolebrand 2011年

现在我已经研究过了,我现在
跌到了

12

C#

并非要简短,但这也许算是创意吗?

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(0, 100).Select(i =>
            string.Format(
                string.Format(
                    "{0} {1} {{3}} {{4}},{{9}}{0} {1} {{3}}.{{9}}{2},{{9}}{3} {4} {{3}} {{4}}.{{9}}",
                    i == 99 ? "{0}" : "{7}",
                    i == 98 ? "{1}" : "{2}",
                    i == 99 ? "{6}" : "{5}",
                    i == 98 ? "{0}" : "{8}",
                    i == 97 ? "{1}" : "{2}"
                ),
                "No",
                "bottle",
                "bottles",
                "of beer",
                "on the wall",
                "Take one down, pass it around",
                "Go to the store, buy some more",
                99 - i,
                (198 - i) % 100,
                Environment.NewLine
        ))));
    }
}

请注意,这只是一个声明:)


11

绝对不符合广告素材的条件,但是可以通过一个命令从命令行完成。

perl -e '$i=99;while($i>1){print("$i bottles of beer on the wall, $i bottles of beer.\nTake one down and pass it around, ".--$i." bottles of beer on the wall\n\n");}print("1 bottle of beer on the wall, 1 bottle of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n");'

11

Haskell中,272250,243个字符

(&)=(++)
b 1=" bottle"
b _=b 1&"s"
w=" on the wall"
p n=show n&b n&" of beer"
f n=putStrLn$p n&w&","&p n&".\n"&c(n-1)
c 0="Go to the store and buy some more, "&p 99&w&"."
c n="Take one down and pass it around, "&p n&w&"\n"
main=mapM f[99,98..1]

11

Windows PowerShell(198)

filter b{"$_ bottle$('s'*!!--$_) of beer"}(99..1|%{($_|b)+($w=' on the wall')+", $($_|b)."
"Take one down and pass it around, $(--$_|b)$w.
"})[0..196]
"Go to the store and buy some more, $(99|b)$w."

非常坦率的。

我正在为啤酒瓶使用过滤器,因为过滤function时间更长,并且无论如何调用都需要括号。复数检测(!!--$_)首先将瓶子的数量减一(因此,复数是非零的任何东西),将其转换为布尔值,并与第一个取反!,然后再次取反,因此我们现在有了一个布尔值,描述数量是否需要多个或不。然后在与字符串相乘时将其隐式转换为整数。

内联换行符很有趣。

生成过多的行,然后再减少。


绝对短,这为尝试下去设置了一个标准!!!虽然从我的角度来看,我希望能够避免出现类似196的“魔术数字”,因为它捕获了(99 * 2)-2。对于可以更改瓶子的答案在一个地方算:)看到codegolf.stackexchange.com/questions/2/99-bottles-of-beer/...
Rebmu博士

您想念“在墙上”。在输出的最后一行中,以及取下每个瓶子之后。
Iszi

filter b{"$_ bottle$('s'*!!--$_) of beer"}(99..1|%{($_|b)+($w=' on the wall')+", $($_|b).";"Take one down and pass it around, $(--$_|b)$w. "})[0..196];"Go to the store and buy some more, $(99|b) $w."解决该问题。总计199个字符。(在此$w之前添加一个换行符"
Iszi 2013年

感谢您的修复。那里也有多余的空间,所以数量改为198。
乔伊

10

卷曲19个字符

(需要互联网连接)

curl -L j.mp/eGv9K5

7
我认为这太过分了。
marcog 2011年

8
我认为这是个玩笑。许多解决方案可以隐藏在对Web服务器的查询后面。
Alexandru

3
我还是喜欢这个。
pimvdb

1
如果目标是最短的代码,我会给您一点技巧。
尼尔

9

来自正在学习Perl的超级渴望的新C程序员的几乎正确的反高尔夫技术?

#!/usr/bin/perl
#      ^
#      |
#      |
# That's the Perl interpreter.
# You might need to change this line based on
# your Linux/Unix distribution.

# Pragmas for debugging!
use strict;
use warnings;

# Library dependencies...none! lolz

# Main implementation
my $number_of_bottles_of_beer_on_the_wall = 99; #start with 99 bottles
LOOP: while( $number_of_bottles_of_beer_on_the_wall > 0 )
{
  printf( "%d bottles of beer on the wall, %d bottles of beer\n",
          $number_of_bottles_of_beer_on_the_wall,
          $number_of_bottles_of_beer_on_the_wall,                  );

  if( $number_of_bottles_of_beer_on_the_wall > 1 )
  {
    $number_of_bottles_of_beer_on_the_wall -= 1;
    printf( "Take one down and pass it around, %d bottles of beer on the wall.\n\n",
            $number_of_bottles_of_beer_on_the_wall,                               );
  }
  else
  {
    printf( "Go to the store and buy some more, %d bottles of beer on the wall\n",
            99                                                                     );
    last LOOP;
  }
}

为什么不/usr/bin/env perl呢?
nyuszika7h 2014年

1
@ nyuszika7h有见地的批评!;)
Daniel Standage 2014年

8

的JavaScript(216 228 215)

for(a=99,c=" on the wall";a;)document.write((d=eval(b="(a||99)+' bottle'+(a-1?'s':'')+' of beer'"))+c+", "+d+".<br>"+(--a?"Take one down and pass it around, ":"Go to the store and buy some more, ")+eval(b)+c+".<p>")

编辑:初始版本中只有一个“ 1瓶啤酒”,第3版已被完全重写,注意到一些很酷的技巧,例如(a||99)在最后一行获得99,在(a-1?'s':'')每种情况下都可以使用复数形式,a==1尽管不需要浪费==,并设置值的b内部需要用到它的声明。


似乎您的瓶子/瓶子优化所花费的字符多于节省的字符。
Yahel 2011年

我不确定您的意思,您究竟要更改什么?
aaaaaaaaaaaaaa

这首诗停下来太早了。最后一节应该去"no more bottles of beer on the wall, no more bottles..." -请参阅:99-bottles-of-beer.net/lyrics.html
三月Örlygsson

顺便说一句,我在233个字节管理的完整,正确的版本: for(o,e,n=100,t=" on the wall";n-->-1;)o=e+t+", "+e+".<br>"+(n>-1?"Take one down, pass it around, ":"Go to the store and buy some more, ")+(e=(0>n?99:n||"no more")+" bottle"+(1!=n?"s":"")+" of beer")+t+".<p>",99>n&&document.write(o)
MARÖrlygsson

@MárÖrlygsson欢迎来到站点。在开篇文章中定义了此挑战的歌词。
aaaaaaaaaaaaaa 2014年

7

C

我一定错过了这个问题,所以这是我在其他地方发布的版本。这是基于C quine的版本。编译并运行以获取歌曲的下一行。重复直到无聊。如果代码显示“时间到...”,则在下次作为命令行参数运行时输入啤酒数量。

// Time to go to the shop and get some beer
//
//
//
//
// #####.#####.#####.#####.#####.#####.#####
// ##.#####.#####.#####.#####.#####.#####.##
// #####.#####.#####.#####.#####.#####.#####
// ##.#####.#####.#####.#####.#####.#####.##

char *z [] = {
"void l(char *s,int b){int i;printf(\"// \");for(i=0;i<b;++i)printf(s);",
"printf(\"\\n\");}\nint main(int argc, char *argv[]){\nint i,j,k,x=%d;",
"char*p;\nif(!x&&argc==2)x=atoi(argv[1]);\nif(!x){printf(\"// Time to ",
"go to the shop and get some beer\\n//\\n//\\n//\\n//\\n\");k=7;\n",
"}else{printf(\"// %%d bottles of beer on the wall, %%d bottles of beer",
".\\n\",x,x);printf(\"// Take one down and pass it round, \");\n",
"if(x>1)printf(\"%%d bottles of beer on the wall.\\n//\\n\",x-1);\n",
"else printf(\"no more bottles of beer on the wall.\\n//\\n\");\n",
"k=x>2?x:2;l(\"  ^   \",x);l(\" / \\\\  \",x);l(\"/   \\\\ \",x);",
"l(\"|   | \",x);l(\"|Duf| \",x);l(\"|   | \",x);l(\"----- \",x);}\n",
"for(i=0;i<4;++i){\nprintf(\"// %%s\", i&1 ? \"##.\" : \"\");\n",
"for(j=i&1;j<k;++j)\nprintf(\"%%s#####\",j!=(i&1)?\".\":\"\");\n",
"printf(\"%%s\\n\",i&1?\".##\":\"\");}\nprintf(\"\\nchar *z [] = {\\n\");\n",
"for(i=0;i<sizeof z/sizeof z[0];++i){\nprintf(\"\\\"\");\n",
"for(p=z[i];*p;++p)\nswitch (*p){\ncase '\\n':printf(\"\\\\n\");break;\n",
"case '\\\\':printf(\"%%c%%c\",92,92);break;\n",
"case '%%':printf(\"%%c\",37);break;\ncase '\"':printf(\"%%c%%c\",92,'\"');break;\n",
"default:printf(\"%%c\", *p);break;}\nprintf(\"\\\",\\n\");}\n",
"printf(\"};\\n\");\nfor(i=0;i<sizeof z/sizeof z[0];++i)\n",
"printf(z[i],x?x-1:0);}\n",
};
void l(char *s,int b){int i;printf("// ");for(i=0;i<b;++i)printf(s);printf("\n");}
int main(int argc, char *argv[]){
int i,j,k,x=0;char*p;
if(!x&&argc==2)x=atoi(argv[1]);
if(!x){printf("// Time to go to the shop and get some beer\n//\n//\n//\n//\n");k=7;
}else{printf("// %d bottles of beer on the wall, %d bottles of beer.\n",x,x);printf("// Take one down and pass it round, ");
if(x>1)printf("%d bottles of beer on the wall.\n//\n",x-1);
else printf("no more bottles of beer on the wall.\n//\n");
k=x>2?x:2;l("  ^   ",x);l(" / \\  ",x);l("/   \\ ",x);l("|   | ",x);l("|Duf| ",x);l("|   | ",x);l("----- ",x);}
for(i=0;i<4;++i){
printf("// %s", i&1 ? "##." : "");
for(j=i&1;j<k;++j)
printf("%s#####",j!=(i&1)?".":"");
printf("%s\n",i&1?".##":"");}
printf("\nchar *z [] = {\n");
for(i=0;i<sizeof z/sizeof z[0];++i){
printf("\"");
for(p=z[i];*p;++p)
switch (*p){
case '\n':printf("\\n");break;
case '\\':printf("%c%c",92,92);break;
case '%':printf("%c",37);break;
case '"':printf("%c%c",92,'"');break;
default:printf("%c", *p);break;}
printf("\",\n");}
printf("};\n");
for(i=0;i<sizeof z/sizeof z[0];++i)
printf(z[i],x?x-1:0);}

关于您建议的编辑,@ DreamWarrior,您是否考虑过将代码放入pastebin中,以便将其作为注释发布?
nderscore 2014年

6

Java语言(285)

假定有一个称为print的函数来输出字符串。

b=' of beer on the wall';n=100;while(--n>1)if(n>1)print(n+" bottles"+b+', '+n+" bottles of beer.\nTake one down and pass it around, "+(n-1)+' bottle'+(n-1>1?'s':'')+b+'.\n\n');print("1 bottle"+b+", 1 bottle of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.")

8
我不确定假定的功能是否公平。例如,我的程序可以是b();。假设有一个功能b()可以在墙上打印99瓶啤酒。:)
Chris Laplante

10
Javascript不提供任何标准的输出字符串的方法,如果您在浏览器中运行它,则它没有打印功能,如果您在Rhino中运行它,则可以。

2
document.writeln作品。
克里斯·拉普兰特

4
...对于浏览器

5
JavaScript的IO具有:alert作为输出,prompt作为变量输入和confirm作为布尔输入。它们会影响到UI,但它们是标准的阻塞IO功能。alert并且print长度相同,因此您的代码应该大致相等。
zzzzBov

6

计划(270)

无空格:

(let l((i 99))(let((b" bottle")(c" on the wall")(d"Take one down and pass it around,")(e".\n")(f", ")(g" of beer"))(if(= i 1)(map display`(1,b,g,c,f,1,b,g,e"Go to the store and buy some more, 99",b,c,e))(begin(map display`(,i,b,g,c,f,i,b,e,d,i,b,c,e"\n"))(l(-1+ i))))))

带空格:

    (let l ((i 99))
      (let ((b" bottle")
            (c" on the wall")
            (d"Take one down and pass it around, ")
            (e".\n")
            (f", ")
            (g" of beer"))
        (if (= i 1)
            (map display`(1 ,b ,g ,c ,f ,1 ,b ,g ,e
                          "Go to the store and buy some more, 99" ,b ,c ,e))
            (begin (map display `(,i ,b ,g ,c ,f ,i ,b ,e ,d ,i ,b ,c ,e "\n"))
                   (l (-1+ i))))))

6

Python-很多

酰胺基岩?

print"""99 bottles of beer on the wall, 99 bottles of beer.
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 beer on the wall.

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

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

....

  Ok, this is stupid. First of all, what the brainfuck are the bottles doing on the wall? They're not spiders nor picture frames. And how are they sitting on the wall?

94 bottles of beer on the wall, 9.. oops, they fell down. 94 bottles of beer on the floor, 94 bottles of beer.

  Second.. who the HQ9+ wants to keep track? I think I lost count after drinking the 2nd one...

Take one ... um... up, and pass it around,  .....  er.. a lot of bottles of beer still on the floor.

  Fourthly, what's with this passing around scheme? They're not j..I mean letters, yeah, or boxes of chocolate. We all can just take one and drink it. It's healthier too.

A pile of bottles of beer on the floor, a pile of bottles of beer.
Everyone take one up and drink it, still a whole bunch of bottles of beer on the floor.

  Um.. seventhly, are we really that many in this assembly that we can finish 200 or however many bottles we had in the beginning? Without passing out?

Go to the store and buy some more

  Yeah and who's gonna pay for it? Definitely not me. And how are you going to bring 300 bottles back from the store?
  In your car? Buddy, you're so drunk, you can't even C anything. Go home dude, go home. Take a cab."""

其他参考(对代码有很大帮助):http : //www.youtube.com/watch?v=Y0Z0raWIHXk


5

蟒蛇(318)

我发现这种使Python程序更短的方法:)

exec'eJxtjrFqwzAQQHd/xVVgLCVqSbq5RHO2TtlcgyX7Qk3luyAphP59ZA0thGzi9O7es0bUERyn5DE/+AwOMdTxi0TljLeLmyzQB4GlaaCBg/hkWDigqMb/76aZz0CHHaCPCLaWTpLSTWw2kl7MXmkBTJC+EW7Wey3U9hmzzqU42R/MNMLEt6KFi40R5gQ28JUmndO0ODIkLhdjyjWFc9dfiLxg6Vsx1ZExu36Vddn2miVD2w59R4d9/6d+f8h7Wze3Y+GrS5gpwSjbVlV3Y1BZCg=='.decode('base64').decode('zip')

第一次看到这个.decode.decode东西对我来说是愚蠢的,但是我现在是出于实际目的使用(将Python代码以不显眼的方式放入我的.bashrc中作为函数),所以谢谢。
ixtmixilix 2012年

5
我计算出未压缩的代码有300个字符。为什么要压缩呢?
约翰内斯·库恩

5

Rebmu -167个字符

M N 99 Bdz[cb[n{ bottle}egN 1{s}{}{ of beer}]]loN[cb[b W{ on the wall}C{, }b P{.}lfEZ--n[Nm{Go to the store and buy some more}]{Take one down and pass it around}cBwPlf]]

可以剃掉几个字符,这只是第一次尝试。:)

这是等效的Rebol,其速记方式已经煮沸了。仍然相当有竞争力,尤其是考虑到清晰度:

m: n: 99

b: does [
    combine [n { bottle} either n > 1 {s} {} { of beer}]
]

loop n [
    print combine [
        b w: { on the wall} c: {, } b p: {.} newline

        either 0 == -- n [
            n: m
            {Go to the store and buy some more}
        ] [
            {Take one down, and pass it around}
        ]

        c b w p newline
    ]
]

GitHub上有注释的源代码


5

PHP:285个 240 233 231特性

$i=99;$b=" bottles of beer";$o=" bottle of beer";$c=" on the wall";while($i>1){echo"$i$b$c, $i$b.\nTake one down and pass it around, ".--$i.(($i>1)?$b:$o).$c.".\n\n";}echo"$i$o$c, $i$o.\nGo to the store and buy some more, 99$b$c.";

在这里输出:http : //ideone.com/5fQmcd


4

Python,241个字符

s=""
i=99
b="%d bottl%s of beer"
w=" on the wall"
t="Take one down and pass it around, "
p=q="es"
while i:s+=b%(i,p)+w+", "+b%(i,p)+".\n";i-=1;p=p[:i];s+=t+b%(i,p)+w+".\n\n"
print s[:-64]+"Go to the store and buy some more, "+b%(99,q)+w+"."

4

Ruby,274个字节

对Ruby来说还很新,实际上只是在玩

o =" bottles of beer";w=" on the wall";t="Take one down and pass it around, ";s=" bottle of beer"
99.downto(3){|b|puts"#{b}#{o+w}, #{b}#{o}.\n#{t}#{b-1}#{o+w}.\n\n"}
puts"2 #{o+w}, 2 #{o}.\n#{t}1#{s}#{w}.\n\n1#{s+w}, 1#{s}.\nGo to the store and buy some more, 99#{o+w}."

4

C#(299个字符)

using System;class D{static void Main(){string a="s",z="",w=" on the wall",q=", ",p=".\n",b=" bottle",c=" of beer";for(int O=99;O>=1;)Console.WriteLine(O+b+(O>1?a:z)+c+w+q+O+b+(O>1?a:z)+c+p+(--O>0?"Take one down and pass it around, "+O:"Go to the store and buy some more, 99")+b+(O==1?z:a)+c+w+p);}}

1
您的最后一行显示“ 99瓶”而不是“ 99瓶”,并且您的循环从19而不是99开始(尽管这可能只是一个疏忽)。
Nellius 2011年

@Nellius〜更正〜并感谢您具有竞争力的代码,我重新访问了我的代码,并删除了一些单个插入块,并减少了我自己代码中的一些先前存在的常量。现在顺利降至300以下。
jcolebrand

您可以通过删除“ using System;”将其降低到293。并将“ Console.WriteLine”更改为“ System.Console.WriteLine”
Tester101

@ Tester101〜好电话。还没考虑!
jcolebrand

4

JavaScript(7个函数)

不打高尔夫球。这旨在作为歌曲的(主要)功能实现。

function firstUpper(s) {
    return s.slice(0, 1).toUpperCase() + s.slice(1);
}

function bottles(x) {
    return (x || "no more") + " " + (x == 1 ? "bottle" : "bottles") + " of beer";
}

function wall(x) {
    return bottles(x) + " on the wall";
}

function line1(x) {
    return wall(x) + ", " + bottles(x) + ".";
}

function line2(x, max) {
     return (x ? "take one down and pass it around, " + wall(x - 1) : "go to the store and buy some more, " + wall(max)) + ".";
}

function verse(x, max) {
    return [line1(x), line2(x, max)].map(firstUpper).join("\n") + "\n\n";
}


function song(max) {
    var text = "";
    for(var x = max; x >= 0; x--) {
        text += verse(x, max);
    }
    return text;
}

print(song(99));

4

去(263)

package main
import "fmt"
func main(){b,i,e,r:=fmt.Println,99,"bottles","of beer on the wall"
for i>0{b(i,e,r+",",i,e,r[:7]+".")
if i--;i<2{e=e[:6]}
if i>0{b("Take one down and pass it around,",i,e,r+`.
`)}}
b("Go to the store and buy some more,",99,e+"s",r+".")}

3

PHP-252个字节

$a=" bottles of beer";$b=str_replace("s","",$a);$c=" on the wall";for($i=98;$i;)echo($j=$i+1).$a.$c.", ".$j.$a.".
Take one down and pass it around, ".$i.($i-->1?$a:$b).$c.".

";echo"1".$b.$c.", 1".$b.".
Go to the store and buy some more, 99".$a.$c.".";

我希望明天再压缩一些。


3

红宝石1.9.2p136:223

我不是胆小鬼,你可以看我的; p

b="%d bottle%s of beer"
w=' on the wall'
99.downto(1){|z|s=b%[z,z>1?'s':'']
puts s+w+", "+s+".
"+(z>1?"Take one down and pass it around, "+b%[z-1,z>2?'s':'']+w+".

" :'Go to the store and buy some more, '+b%[99,'s']+w+".")}

3

(Oracle)SQL

没有角色数,我没有打高尔夫球。刚刚发现这是一种有趣的方式。

WITH
   bottles AS (
      SELECT LEVEL - 1 AS bottle 
      FROM dual
      CONNECT BY LEVEL <= &number_of_bottles + 1
   ),
   fragments AS (
      SELECT
         'no more ' AS none,
         'bottles of beer' AS supply,
         ' on the wall' AS wall,
         'Take one down and pass it around' AS drink,
         'Go to the store and buy some more' AS refill,
         CHR(13) || CHR(10) AS newline
      FROM dual
   ),
   combined AS (
      SELECT
         b.bottle,
         DECODE(
            b.bottle, 
            1, b.bottle || ' ' || REPLACE(f.supply, 's'),
            0, f.none || f.supply,  
            b.bottle || ' ' || f.supply
         ) AS supply
      FROM bottles b
      CROSS JOIN fragments f

   ),
   two_lines AS (
      SELECT LEVEL AS line
      FROM dual
      CONNECT BY LEVEL <= 2
   )
SELECT
   CASE l.line
      WHEN 1 THEN REPLACE(c1.supply, 'n', 'N') || f.wall || ', ' || c1.supply || '.'
      WHEN 2 THEN DECODE(b.bottle, 0, f.refill, f.drink) || ', '  || c2.supply || f.wall || '.' 
   END AS song 
FROM bottles b
LEFT JOIN combined c1 ON (c1.bottle = b.bottle)
LEFT JOIN combined c2 ON (c2.bottle = DECODE(b.bottle - 1, -1, &number_of_bottles, b.bottle - 1))
CROSS JOIN two_lines l
CROSS JOIN fragments f
ORDER BY
   b.bottle DESC,
   l.line;
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.