简体   繁体   English

对 C++ 递归感到困惑

[英]Confused about C++ recursion

I don't understand how recursion works too well.我不明白递归如何工作得很好。

void f(int n)
{
  if (n == 1)cout<<1<<" ";
  else
  {
    f(n - 1);
    cout<<n<<" ";
    f(n - 1);
  }

If i let n = 4, this will output 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 .如果我让 n = 4,这将输出1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 Why is that?这是为什么? First, n gets smaller and smaller until it gets to 1, and after that, what happens?首先, n越来越小,直到达到 1,然后,会发生什么? I can't really understand what these 2 calls do, and why even the second one is there, since the first one gets called first.我无法真正理解这两个调用的作用,以及为什么第二个调用还在那里,因为第一个调用首先被调用。

Recursive functions work exactly like non-recursive functions.递归函数的工作方式与非递归函数完全一样。
In particular, when one returns it returns to its immediate caller.特别是,当一个返回时,它返回到它的直接调用者。
That is, the call that had n == 1 will return to a call that had n == 2 , which will return to a call that had n == 3 , and so on, and the calling function keeps going in the regular way.也就是说,具有n == 1的调用将返回具有n == 2的调用,后者将返回具有n == 3的调用,依此类推,并且调用函数继续以常规方式运行.

Your example works like these non-recursive functions, whose flow you can probably figure out:您的示例就像这些非递归函数一样工作,您可能会弄清楚其流程:

void f_1()
{
    cout << 1 << " ";
}

void f_2()
{
    f_1();
    cout << 2 << " ";
    f_1();
}

void f_3()
{
    f_2();
    cout << 3 << " ";
    f_2();
}

void f_4()
{
    f_3();
    cout << 4 << " ";
    f_3();
}

The algorithm is, when reaches the end of recursion:该算法是,当到达递归结束时:

  • Print n打印n
  • Return and print the n + 1 who called n返回并打印调用nn + 1
  • Print the first n again再次打印前n

Here, the n when the recursion ends is 1 .这里,递归结束时的n1 That's why every other number in your sequence is 1 (corresponds to the 1st and 3rd points)这就是为什么序列中的每个其他数字都是 1(对应于第 1 点和第 3 点)

If you remove the 1 's, your sequence is:如果删除1 ,则序列为:

2 3 2 4 2 3 2

Now, the n when the recursion "ends" is 2 (obviosuly it ended on 1 but now we're going one level above), so we repeat the algorithm.现在,递归“结束”时的n2 (显然它以1结束,但现在我们要上一层),所以我们重复算法。

If you remove the 2 s...如果您删除2秒...

3 4 3

Again, 3 is the n ... and your highest level is the remaining 4 .同样, 3n ... 而你的最高级别是剩下的4

You see a symmetric sequence due to your symmetric algorithm:由于您的对称算法,您会看到对称序列:

    f(n - 1);
    cout<<n<<" ";
    f(n - 1);

Here below a recursive explanation of what is happening.下面是对正在发生的事情的递归解释。

When n gets down to n=1 the if part kicks in, prints 1, then returns.n下降到n=1if部分开始,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 2 , that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = 2 .返回时,我们返回到n的前一次迭代, n = 2 ,现在从else语句的第一行移到第二行,打印 2,然后在第三行,这是另一个重新启动的递归调用n = 2的函数。

So we go through the first statement of else again, we get n = n - 1 = 1 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 1

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 2 , that now moves out of the third line of the else statement and returns.返回时,我们返回到n的前一次迭代, n = 2 ,现在移出else语句的第三行并返回。

On return, we are brought back to the previous iteration of n , n = 3 , that now moves out of the first line of the else statement down to the second, which prints 3, then on the third which is another recursive call that restarts the function with n = n - 1 = 2 .返回时,我们回到n的前一次迭代, n = 3 ,现在从else语句的第一行移到第二行,打印 3,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 2的函数。

So we go through the first statement of else again, we get n = n - 1 = 1 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 1

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 2 , that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1 .返回时,我们返回到n的前一次迭代, n = 2 ,现在从else语句的第一行移到第二行,打印 2,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 1的函数。

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 4 , that now moves out of the first line of the else statement down to the second, which prints 4, then on the third which is another recursive call that restarts the function with n = n - 1 = 3 .返回时,我们回到n的前一次迭代, n = 4 ,现在从else语句的第一行移到第二行,打印 4,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 3的函数。

So we go through the first statement of else again, we get n = n - 1 = 2 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 2

So we go through the first statement of else again, we get n = n - 1 = 1 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 1

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 2 , that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1 .返回时,我们返回到n的前一次迭代, n = 2 ,现在从else语句的第一行移到第二行,打印 2,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 1的函数。

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 3 , that now moves out of the first line of the else statement down to the second, which prints 3, then on the third which is another recursive call that restarts the function with n = n - 1 = 2 .返回时,我们回到n的前一次迭代, n = 3 ,现在从else语句的第一行移到第二行,打印 3,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 2的函数。

So we go through the first statement of else again, we get n = n - 1 = 2 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 2

So we go through the first statement of else again, we get n = n - 1 = 1 .所以我们再次通过else的第一条语句,我们得到n = n - 1 = 1

When n gets down to n = 1 the if part kicks in, prints 1, then returns.当 n 下降到n = 1if部分开始工作,打印 1,然后返回。

On return, we are brought back to the previous iteration of n , n = 2 , that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1 .返回时,我们返回到n的前一次迭代, n = 2 ,现在从else语句的第一行移到第二行,打印 2,然后在第三行,这是另一个重新启动的递归调用n = n - 1 = 1的函数。

When n gets down to n = 1 the if part kicks in, prints 1, then returns.n下降到n = 1if部分开始工作,打印 1,然后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM