为什么此函数按升序打印 1 2 3 4 5
Why does this function print 1 2 3 4 5 in ascending order
我尝试搜索答案,但无法得到任何令人信服的答案。有人可以解释一下这个 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/
- 回溯C++不打印函数,因此文件
- 重写打印函数而不是覆盖基类
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 打印传递给打印函数的矢量
- 如何使用重新定义的打印函数打印Lua表?
- 调用和打印函数,在 C++ 中返回数组
- Lua C API 自定义打印函数,在字符串中传递空格时不调用
- C++二叉树打印函数实现
- 如何从主 C++ 向模板类中的方法发送打印函数
- 如果我不打印函数参数的值,则将其转换为等于零
- 打印函数模板的内容进行类指针
- C++打印函数的结果?
- 打印 C++ 函数
- 编写一个通用打印函数来打印数组
- 用于打印函数进入和退出跟踪的宏
- 每当尝试打印函数变量时,C++ cout 错误
- GDB:当没有可用的名称符号时,如何打印函数参数值
- 在lua中重新定义打印函数时出现问题
- 打印函数会返回一个意外值,原因是什么
- IntStack的C++递归打印函数