Recursive algorithm 1-simple recursion to find the factorial of n

Recursion is to call yourself. It is a powerful tool for designing and describing algorithms. It is often used to solve more complex problems. Algorithms that can be described by recursion usually have the following characteristics:
To solve a problem of scale N, try to decompose it It is easy to construct solutions to large problems from the solutions of small problems, and these small problems can also be decomposed into smaller problems using the same decomposition method, and can be resolved from these smaller problems The solution of constructs the solution of a larger problem. In general, when the scale N=1, the solution of the problem is known.

Is recursion a method of divide and conquer, transforming complex problems into simple ones? The recursive algorithm has the following advantages and disadvantages:


Advantages: The program written by recursion is concise and the structure is clear. The correctness of the program is easy to prove, and there is no need to understand the specific details of recursive calls.

Disadvantages: In the process of calling a recursive function, each layer of call needs to save temporary variables and return addresses, and pass parameters, so the execution efficiency of the recursive function is low.

Simple recursion
Finding the factorial of n, Fibonacci sequence, finding the maximum of n numbers, number conversion, finding the greatest common divisor are all simple recursion.

Find the factorial of n

[Analysis] The
recursive process is divided into two stages: recursive and recursive. Recursion is to find the most basic problem solution according to the problem to be solved. This process requires the system stack to save the value of temporary variables; recursion is to obtain the solution of the problem according to the solution of the most basic problem. This process is to gradually release the stack space , Until the problem is solved.

The process of finding the factorial of n is divided into backstepping and recursion.

1. push back

Finding the factorial of n can be described as follows:
n!=n*(n-1)!
(n-1)!=(n-1)*(n-2)!
(n-2)!=(n-2) *(n-3)!
(n-3)!=(n-3)*(n-4)!
...
2!=2*1!
1!=0!
0!=1
1!=1

If n! is written in functional form, that is, f(n), then f(5) means 5!. The process of finding 5! can be written as follows:
f(5)=5*f(4)
f(4)=4*f(3)
f(3)=3*f(2)
f(2)=2* f(1)
f(1)=1

From the above process, we can see that to find f(5) you need to call f(4), to find f(4) you need to call f(3), and to find f(3) you need to call f(2), and f(2) ) Needs to call f(1). Among them, f(5), f(4), f(3), f(2), f(1) will all call the same function f, but the parameters are different. The process of the above recursive call is shown in the figure.

2. Recursion

According to the most basic known condition of f(1), 2!, 3!, 4!, 5! are obtained. This process is called recursion. The final result can be obtained by the recursive process as shown in the figure.


In summary, the process of recursion is to turn a complex problem into a simple one, and the process of recursion is to obtain a solution to a complex problem from a simple problem.


【Algorithm Description】

Through the above analysis, when n=0 or n=1, the factorial of n is f(n)=1; otherwise, the factorial of n is f(n)=n*f(n-1). So the formula is:

f(n)= \begin{cases} 1 & n=0,1 \\ n*f(n-1) & n=2,3,4,... \end{cases}
In fact, it is a recursively defined formula.

code:

#include<stdio.h>
#include <iostream>
long int Fact(int n);
void main()
{
	int n;
	printf("请输入一个整数:");
	scanf("%d", &n);
	printf("%d!=%d\n", n, Fact(n));
	system("pause");
}
long int Fact(int n)
{
	int x;
	long int y;
	if (n < 0)                  	/*n<0时阶乘无定义*/
	{
		printf("参数错!");
		return -1;
	}
	if (n == 0)					/*n==0时阶乘为1*/
		return 1;
	else
	{
		return n*Fact(n - 1);		/*递归求n的阶乘*/
	}
}

result:

 

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/104130503