为什么此函数按升序打印 1 2 3 4 5

Why does this function print 1 2 3 4 5 in ascending order

本文关键字:打印 函数 升序 为什么      更新时间:2023-10-16

我尝试搜索答案,但无法得到任何令人信服的答案。有人可以解释一下这个 c++ 代码是如何打印1 2 3 4 5的吗?

我明白直到n=1 n=1时,fun(n-1) = fun(1-1) = fun(0)没有执行,因为n>0不满意,因此现在cout << n << endl将被执行并且n仍然等于1但是打印1后它应该停止吗?它如何转到以前的呼叫?它如何打印2 3 4 5

此外,当cout << n << endl高于fun(n-1)时,它打印5 4 3 2 1是有意义的。

#include <iostream>
using namespace std;
void fun(int n)
{
    if (n > 0) {
        fun(n - 1);
        cout << n << endl;
    }
}
int main()
{
    int x = 5;
    fun(x);
}

上面的代码打印1 2 3 4 5而据我了解,它应该只打印1

在每次调用函数时,fun()记下变量n的值,您可以轻松跟踪输出。当递归调用返回fun()时,它之后的语句将被执行(w.r.t. 函数fun()它是cout << n << endl;语句(。它的工作原理是这样的:

fun(5)    -->   First call : n is 5
5>0 : true
|   fun(5-1)    --> Recursive call 1 : n is 4
|   4>0 : true
|   |   fun(4-1)    --> Recursive call 2 : n is 3
|   |   3>0 : true
|   |   |   fun(3-1)    --> Recursive call 3 : n is 2
|   |   |   2>0 : true
|   |   |   |   fun(2-1)    --> Recursive call 4 : n is 1
|   |   |   |   1>0 : true
|   |   |   |   |   fun(1-1)    --> Recursive call 5 : n is 0
|   |   |   |   |   0>0 : false  --> Return from call 5
|   |   |   |   |
|   |   |   |   Print n (n is 1) and return from call 4
|   |   |   Print n (n is 2) and return from call 3
|   |   Print n (n is 3) and return from call 2
|   Print n (n is 4) and return from call 1
Print n (n is 5) and return from first call to main()

因此,输出是 1 2 3 4 5 .

这里要认识到的关键是,fun()递归函数在进行递归调用打印值。 所以这是实际发生的事情:

call fun(5) from main()
call fun(4) from fun()
call fun(3) from fun()
call fun(2) from fun()
call fun(1) from fun()
return from fun(0)
print 1
print 2
print 3
print 4
print 5

也就是说,print语句发生递归调用,从递归底部的值开始,即1,然后退到5

好吧,这里的关键是函数将在递归调用继续执行,因此:

fun(n - 1);
cout << n << endl;

将调用fun(n-1),并在fun(n-1)返回后继续执行cout << n << endl;。它不会消失。它只会写入的执行推迟到cout,直到递归调用返回之后。

我不清楚您希望使用此递归函数完成什么,但是如果您只想打印1,则需要将其封装在if语句中:

if(n == 1) {
    cout << n << endl;
}

或者做其他事情来达到这个效果。如果这不是你想要的,你可能需要完全重写你的递归函数(如果你不确定如何做,你可能需要问另一个问题,更明确地解释你的需求(。

遵循执行:

int x = 5;
fun(5);     // Since x = 5
if (5 > 0)  // Since n = 5
fun(4);     // Since n - 1 = 4
if (4 > 0)  // Since n = 4
fun(3);     // Since n - 1 = 3
if (3 > 0)  // As before...
fun(2);
if (2 > 0)
fun(1);
if (1 > 0)
fun(0);
if (0 > 0)  // If fails, so function exits
cout << 1 << endl;  // Then returns
cout << 2 << endl;  // Then returns
cout << 3 << endl;  // ...
cout << 4 << endl;
cout << 5 << endl;

当我试图理解一些复杂的算法时,我总是以上述格式写下执行。该方法适用于新手和更有经验的程序员。

首先,您应该将 end 的条件更改为 (n>1(,然后,您可以得到结果 [2 3 4 5]。 看这里。

   #include     使用命名空间标准;    虚空乐趣(int n(    {        if (n> 1( {            乐趣(n - 1(;            cout <<n <<endl;           }    }    int main((    {        整数 x = 5;        乐趣(x(;    }

其次,如果您更改行位置,则可以获得 revese 结果 [5 4 3 2 1]。 看这里。

   #include     使用命名空间标准;    虚空乐趣(int n(    {        如果 (n> 0( {            cout <<n <<endl;            乐趣(n - 1(;        }    }    int main((    {        整数 x = 5;        乐趣(x(;    }
我想

,而不是问这个确切的功能。请了解递归的工作原理。那里有很多资源。您还可以关注: https://www.geeksforgeeks.org/recursion/