99 Bottles of Beer

『99 Bottles of Beer』は、カナダ、アメリカの伝統的な数え歌である。

コンピュータサイエンスの分野では、しばしばこの歌の歌詞を出力する課題が与えられる。

 歌詞 
歌詞は単純で、ビールの本数が1本ずつ減っていき、最後には0になる。
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.

...

1 bottle of beer on the wall, 1 bottle of beer.

Take one down and pass it around, no more bottles of beer on the wall.

 No more bottles of beer on the wall, no more bottles of beer.

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

 コード 
以下はこの歌の歌詞を出力するコード例である。

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    for (int i = 99; i >= 0; --i) {
        if (i > 1) {
            cout << i << " bottles of beer on the wall, " 
                 << i << " bottles of beer.\n";
            cout << "Take one down and pass it around, " 
                 << i - 1 << (i - 1 == 1 ? " bottle" : " bottles")
                 << " of beer on the wall.\n\n";
        } else if (i == 1) {
            cout << "1 bottle of beer on the wall, 1 bottle of beer.\n";
            cout << "Take one down and pass it around, no more bottles of beer on the wall.\n\n";
        } else {
            cout << "No more bottles of beer on the wall, no more bottles of beer.\n";
            cout << "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
        }
    }
    return 0;
}HQ9+9

Category:数え歌
Category:アメリカ合衆国の民謡
Category:酒を題材とした楽曲