Beginners see: simply talk about CC ++ recursive ideas, implementations, and relationships with cycles.

zhaozj2021-02-11  232

Many beginners often have a lot of time to recursively, and it has spent a lot of time. In fact, the examples on the textbook are very classic, but it has some embarrassed. The head of beginners will look at the head. Programming is solving the problem, and many problems in reality are relatively simple, and there is no complexity like Hano Tam. We don't have to pursue how to be achieved, we just have to use recursive, and will solve some problems with our return, this is.

First look at an example:

There is a Febonacci sequence:

1, 1, 2, 3, 5, 8, 13,, 21, 34 ........

Its problem is: ask the n number in this sequence.

Since its function is: f (n) = f (n-1) f (n-2)

This can be written with recursive, you can't afford it at all:

INT febc (int N) {

IF (n <3) return (1);

Else

Return (Febc (N-1) Febc (N-2));

}

Oh ~~~~~ Maybe you will say that it is too simple, it is simply a mathematical model, huh, huh.

In fact, the work process of recursive functions is to call themselves. There are some questions to be able to solve it with recursive, and you will be shocked by yourself.

We do things, usually start from scratch, but recursive starts from the end. For example, the function above, when n> 3, it is obviously only for N-1, N-2. (N-1)> 2, (N-2)> 2, they help: (N-1) -1, (N-1) -2; (N-2) -1, (N- 2) -2; then ··········· until (NK) <3, (NK-1) <3, the function febc finally has a return value of 1, it starts from the beginning Calculate, and then it is still to N.

Through the above example, we know that the recursive must have a stop condition, otherwise it will not know if it is stopped. In the above example, IF (n <3) return (1); is the condition where the stop is stopped.

However, the cost of using recursive is very huge: it will consume a lot of memory! ! When recursive cycles, it uses a stack, and the resource of the stack is very limited. The above example can only use a small N value. If n = 20, that is, Febc (20), it will call the Febc (n) function more than 10,000 times! ! ! And one of the above examples is also very easy to write:

/ * using turboc2 * / int feed (int); main () {int N; scanf ("% d", & n); febc (n);}

INT FeBC (INT N) {Int A [3], I; A [0] = a [1] = a [2] = 1; for (i = 3; i <= n; i ) a [i% 3 ] = a [(i 1)% 3] a [(i 2)% 3]; / * Implement FeBC (I) = FeBC (I-1) Febc (I-2) * / Printf (" / N% D / N ", A [N% 3]);

Interests may wish to enter a larger n value and then compare the speed of this two functions. Of course, if you use N too big, recursive may have errors. If you crash, don't yell, oh ~~~ I have remind you :)

Now let's take a look at a loop from 1 to 100:

/ * turboc2 * /

Main ()

{INT I, N;

For (i = 1; i <101; i )

N = I;

This is very simple. However, can you write the corresponding recursive function? Here is recuing (please pay attention, this practice is not recommended !! I just wrote this for explaining the problem.)

/ * using turboc2 * /

INT A = 0; int access (int); main () {account (100); Printf ("% d", a);} int access (int i) {if (i == 0) returnography; / * Stop condition * / else a = Account (i-1) 1; / * Implement recursive * /}

In the problem of C / C , I once answered such a question:

If a small cow, from birth to a fourth year, every year, every year, according to this law, how many cows are there in the nine? How do I use the recursive culfment?

First write function expression: f (n) = f (n-1) f (n-3)

Why f (n) = f (n-1) f (n-3), please see: f (n) -f (n-1) = f (n-3) Because the n-year is more than N- More than a year of cattle is a large than three years old, and f (n-3) is those who are more than three years old, then they are in the n-year. The same quantity of calves. (Please use Borland C 3.1 or other C compiler)

#include #include int Cattle (int, int); void main () {int CT, N; cout << "please input the Original Cattle Number:" << endl; / * Enter the number of start cattle * / cin >> ct; cout << "Input how much Years it pas: << Endl; cin >> n; cout <<" you have "<< Cattle (CT, N) < <"Cattle Now!" << Endl; getch ();} int Cattle (int CT, INT N) {IF (n <4) Return (CT); / * Stop condition * / else return (Cattle (CT, N) -1) Cattle (CT, N-3)); / * Implement recursive * /}

How is it, very simple. Will you solve it with a cycle?

Regeneration is not commonly used in actual programming, but in some cases it is very powerful and beautiful tool. It is very useful when you master it.

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

New Post(0)