Simple explanation of recursive

zhaozj2021-02-16  48

The simplest recursive has such a form

Fn = a | fn

Its result is A

The calculation process is as follows, it is a mathematical inductive method.

Recursor number) Expression 1) Fn = a2) Fn = fn = a .... n) fn = fn = ... (n - 1 time) = fn = a

So fn = a

Write into C language is: #define a 1

INT FN (INT N) {IF (n == 0) RETURN A; RETURN FN (N - 1)}

n == 0 is a recursive termination condition. When it is enabled, A is called an absorbent sub-, and Fn is a recursive process.

When the absorbers do not contain a recursive process (direct or indirect), it is referred to as limited recursive. The recursion in programming must be limited recursive.

Regardless of how the recursive process is defined, it produces an absorption. The entire recursive process is actually generating a parameter number A = = {Fn (n = 1), Fn (n = 2), .... fn (n = N-1)}. The result is a function of argument as an argument by the absorption sub-and parameter number: f (a, a).

In the above example, the parameters of Fn (n) are n, and the parameter number is a = {n, n - 1, n - 2, ..., 0} function f (x, y) = x. So the result is 0.

Again for example:

Void Fn (int N, int sum) {IF (n == 0) return;

Printf ("N =% D, SUM =% D", N, SUM) Fn (N - 1, SUM N); Printf ("N =% D, Sum =% D", N, Sum)}

This recursive process A = = {n, sum}, {n - 1, sum n}, {n - 2, sum n n - 1}, ... {0, SUM N * N - 1 - 2 ... - (n - 1)} a = empty

F (x, y) = empty.

The result is just to print a number of A number

However, this example illustrates a problem, f (x, y) actually works twice. The first time A order, the second time A is in the reverse order.

The first time occurred in the generation of a diameter, the second occurs in the process of destroying A. In fact, it illustrates the recursive stack nature.

That is to say, all recursive functions can be rewritten into non-recursive functions (use stacks). As the topic:

Struct a {int n; int sum;};

#define n = 30; // Maximum stack deep void fn (int N, int sum) {a stack [n]; int i = n;

Stack [n] .n = n; stack [n] .sum = sum; printf ("n =% D, sum =% D", stack [i] .n, stack [i] .sum);

While (i) {stack [i - 1] .sum = stack [i] .sum i; stack [i - 1] .n = i;

I-; Printf ("n =% D, SUM =% D", Stack [i] .n, stack [i] .sum);}

While (i

{

Printf ("N =% D, SUM =% D", Stack [i] .n, stack [i] .sum)

i ;

}

}

Can be found in the program:

#include

Void Fn (int N, int sum) {IF (n == 0) return;

Printf ("Before N =% D, SUM =% D / N", N, SUM); Fn (N - 1, Sum N); Printf ("After n =% D, Sum =% D / N", n, sum);} struct a {int n; int sum;};

#define n 30 // Best stack deep

Void Fn2 (int N, int sum) {a stack [n]; int i = n;

Stack [n] .n = n; stack [n] .sum = sum;

While (i) {stack [i - 1] .sum = stack [i] .sum i; stack [i - 1] .n = i - 1; printf ("Before n =% D, SUM =% D / N ", Stack [i] .n, stack [i] .sum); i-;}

While (i

}

INT main () {fn (5, 0); FN2 (5, 0);}

转载请注明原文地址:https://www.9cbs.com/read-15622.html

New Post(0)