【C++】递归(1)--包含一个递归调用的递归理解

版权声明:本文为博主原创文章,未经允许,不得转载!欢迎留言附带链接转载! https://blog.csdn.net/qq_15698613/article/details/89307208

总体思维:

递归是一层一层进去,然后再从最里面一层一层出来 ;比如你买了一个俄罗斯套娃,一共3层,大白+中黑+小红

大-->>中-->>小这样进来,然后红-->>黑-->>白  出来!

1.递归调用的递归 

void recurs(argumentlist)
{
	statements1
	if (test)
		recurs(arguments)
	statements2
}
// recur.cpp -- using recurion
//递归的简单理解使用--by Learning_CV 2019/04/15
#include<iostream>
void countdown(int n);

int main()
{
	countdown(4);
	return 0;
}

void countdown(int n)
{
	using namespace std;
	cout << "计数  ... " << n << endl;
	if (n > 0)
		countdown(n - 1);
	cout << n << ": 回退!\n";
}

运行结果:

理解:

 

将地址输出看一下 

// recur.cpp -- using recurion
//递归的简单理解使用--by Learning_CV 2019/04/15
#include<iostream>
void countdown(int n);

int main()
{
	countdown(4);
	return 0;
}

void countdown(int n)
{
	using namespace std;
	cout << "计数  ... " << n << "( n 地址 " << &n <<  ")"<<endl;
	if (n > 0)
		countdown(n - 1);
	cout << n << ": 回退!" <<  "( n 地址 " << &n << ")"<<endl;
}

 

猜你喜欢

转载自blog.csdn.net/qq_15698613/article/details/89307208