了解递归在啤酒瓶的例子 [英] Understanding recursion in the beer bottle example

查看:195
本文介绍了了解递归在啤酒瓶的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C对我自己练递归,我发现这个例子联机。
然而,有一件事我不明白。

I am practicing recursion in C on my own and I found this example online. However there is one thing I don't understand.

void singSongFor(int numberOfBottles)
{
if (numberOfBottles == 0) {
    printf("There are simply no more bottles of beer on the wall.\n\n");
} 
else {
    printf("%d bottles of beer on the wall. %d bottles of beer.\n",
           numberOfBottles, numberOfBottles);
    int oneFewer = numberOfBottles - 1;
    printf("Take one down, pass it around, %d bottles of beer on the wall.\n\n",
           oneFewer);
    singSongFor(oneFewer); // This function calls itself!

    // Print a message just before the function ends
    printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
             numberOfBottles);
   }
}    

然后我用一个main方法这样:

Then I use a main method as such:

 int main(int argc, const char * argv[])
{
  singSongFor(4);
  return 0;
}

和输出是这样:

在墙上的4瓶啤酒。 4瓶啤酒。
就拿一台,通过它周围,3瓶啤酒在墙上。

4 bottles of beer on the wall. 4 bottles of beer. Take one down, pass it around, 3 bottles of beer on the wall.

在墙壁3瓶啤酒。 3瓶啤酒。
就拿一台,通过它周围,2瓶啤酒在墙上。

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

在墙壁上2瓶啤酒。 2瓶啤酒。
就拿一台,通过它周围,啤酒瓶1在墙壁上。

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

在墙壁上1瓶啤酒。 1瓶啤酒。
就拿一台,通过它周围,啤酒瓶0在墙壁上。

1 bottles of beer on the wall. 1 bottles of beer. Take one down, pass it around, 0 bottles of beer on the wall.

有在墙壁上的啤酒根本就没有更多的瓶子。

There are simply no more bottles of beer on the wall.

将一个瓶子回收,1空瓶的箱子。

Put a bottle in the recycling, 1 empty bottles in the bin.

将一个瓶子回收,2空瓶的箱子。

Put a bottle in the recycling, 2 empty bottles in the bin.

将一个瓶子回收,3个空瓶的箱子。

Put a bottle in the recycling, 3 empty bottles in the bin.

将一个瓶子回收,4空瓶的箱子。

Put a bottle in the recycling, 4 empty bottles in the bin.

据我所知,第一部分非常好,直到我来有没有简单的在墙壁上的啤酒瓶多。我不明白怎么算账瓶子的变量数从1到增加4。

I understand the first part very well until I come to "There are simply no more bottles of beer on the wall. I don't understand afterwards how the variable number of bottles is incremented from 1 till 4.

推荐答案

请注意,最后的printf 使用 numberOfBottles 变量,那就是永远不会修改。因此,在从印刷返回 oneFewer 瓶,将打印与 numberOfBottles 回收文本。记住,每间每次呼叫的局部变量函数的一个不同的化身。

Note that the last printf uses the numberOfBottles variable, and that is never modified. So upon returning from printing oneFewer bottles, it will print the recycling text with numberOfBottles. Remember that there is one different incarnation of the local variables per each call to the function.

它可以,如果你要缩进的功能调用可以看到简单:

It can be seen easier if you indent the calls to the functions:

4 bottles of beer on the wall...
  3 bottles of beer on the wall...
    2 bottles of beer on the wall...
      1 bottles of beer on the wall...
        There are simply no more bottles of beer on the wall.
      Put a bottle in the recycling, 1 empty bottles in the bin.
    Put a bottle in the recycling, 2 empty bottles in the bin.
  Put a bottle in the recycling, 3 empty bottles in the bin.
Put a bottle in the recycling, 4 empty bottles in the bin.

现在,每次在同一列开始线从功能的同一呼叫写入。你怎么看瓶和回收coindice多少?这是因为两者都使用相同的变量: numberOfBottles

Now, each line that begins in the same column is written from the same call of the function. Do you see how the number of bottles and recycling coindice? That's because both use the same variable: numberOfBottles.

这篇关于了解递归在啤酒瓶的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆