search

Home  >  Q&A  >  body text

objective-c - 关于一个递归代码的问题~

最近在学习objective-c,教程中有一段代码没弄清楚,是关于递归的,我用截图的形式发上来,给大家看看

void singTheSong(int numberOfBottles)
{
    if (numberOfBottles == 0) {
        printf("there are simply no more bottles of beer on the wall.\n");
    } else {
        printf("%d bottles of beer on the wall. %d bottles of beer.\n",
               numberOfBottles, numberOfBottles);
        int oneFewer = numberOfBottles - 1;
        printf("Take one dowm, pass it around, %d bottles of beer on the wall.\n",
               oneFewer);

        singTheSong(oneFewer);
        printf("put a bottle in the recycling, %d empty bottles in the bin.\n",
               numberOfBottles);
    }
}

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

它的输出结果是这样的:

99 bottles of beer on the wall. 99 bottles of beer.
Take one dowm, pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall. 98 bottles of beer.
Take one dowm, pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall. 97 bottles of beer.
Take one dowm, pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall. 96 bottles of beer.
Take one dowm, pass it around, 95 bottles of beer on the wall.
......(中间重复的省略)
1 bottles of beer on the wall. 1 bottles of beer.
Take one dowm, pass it around, 0 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, 98 empty bottles in the bin.
put a bottle in the recycling, 99 empty bottles in the bin.
Program ended with exit code: 0

这段代码,我看不懂的地方是,它如何从numberOfBottles等于0的时候,又继续运行了

printf("put a bottle in the recyling, %d empty bottles in the bin.\n",
        numberOfBottles);

这段代码呢?并且numberOfBottles一直在+1

请大家帮我解惑,谢谢了~

黄舟黄舟2768 days ago538

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-22 09:02:04

    The key point is: you need to understand that function calls will have 压栈操作, 递归调用返回时会回到递归之前, 并继续code after executing recursive calls.

    Tell me your singTheSong(99);替换成singTheSong(3);.

    入参为3, 下面两行打印
    3 bottles of beer on the wall. 3 bottles of beer.
    Take one dowm, pass it around, 2 bottles of beer on the wall.

    The first recursive call to singTheSong(2), the input parameter is 2.第一次递归调用singTheSong(2), 入参为2.
    注意, 此时singTheSong(oneFewer); 之后的printf打印不会出现, 因为递归调用还没有返回;
    后面你看到的 numberOfBottles一直在+1 现象的直接原因就在这.Note that at this time, the printf printing after singTheSong(oneFewer); will not appear because the recursive call has not yet Return;

    The numberOfBottles you see later is always +1. This is the direct cause of the phenomenon.

    第二次递归调用singTheSong(1), 入参为1
    同样, singTheSong(oneFewer) 之后的printf打印不会出现; 2 bottles of beer on the wall. 2 bottles of beer.
    Take one dowm, pass it around, 1 bottles of beer on the wall.

    The second recursive call to singTheSong(1), the input parameter is 1

    Similarly, the printf after singTheSong(oneFewer) will not appear;第三次递归调用singTheSong(0), 入参为0, 直接打印下面一行, 并退出;
    注意, 这里的退出是退回到第二次递归的栈. 并从singTheSong(oneFewer)之后的printf开始执行. 1 bottles of beer on the wall. 1 bottles of beer. Take one dowm, pass it around, 0 bottles of beer on the wall.

    The third recursive call singTheSong(0), the input parameter is 0, directly print the following line, and exit;

    因为第二次递归入参为1, 所以这里打印下面一行
    第二次递归调用退回到 第一次 递归的栈继续执行Note that the exit here is to return to the stack of the second recursion . And start execution from the printf after singTheSong(oneFewer).

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

    因为第一次递归入参为2, 所以这里打印下面一行
    第一次递归调用退回到最初的singTheSong(3)继续执行Because the input parameter of the second recursion is 1, the following line is printed here
    The second recursive call returns to the first recursive stack and continues execution

    put a bottle in the recycling, 1 empty bottle in the bin.

    最初的singTheSong(3)调用入参为3, 所以打印下面一行.Because the first recursive input parameter is 2, the following line is printed here
    The first recursive call returns to the original singTheSong(3) to continue execution

    put a bottle in the recycling, 2 empty bottles in the bin.🎜 🎜🎜🎜 put a bottle in the recycling, 3 empty bottles in the bin.🎜

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-22 09:02:04

    The execution order problem did not occurnumberOfBottles一直在+1的情况
    singTheSong(99);执行时需要调用singTheSong(98);但此时singTheSong(99);and the execution was not completed

    printf("put a bottle in the recyling, %d empty bottles in the bin.\n",
            numberOfBottles);
    

    This code is not called until singTheSong(0);运行完成之后,singTheSong(1);方法体中的最下面的printfThe code is executed, and then recursively, starting to execute 2, 3, 4... 99

    reply
    0
  • Cancelreply