So know recursion, you will suddenly realize! If you have a very thorough understanding of recursive drifting ~

The concept of recursive algorithm is a function of a certain function calls themselves to achieve, in the following way to understand recursion you will suddenly realize (if you have doubts, then recursion), and

I think recursion is so simple:

1. Recursion is the number of columns in the chapter on high school mathematics was talking about. The number of columns this chapter introduces a concept called recursion formula: item 1 (or the first few rows If the given item ) , and second from the start of any (or an item) a An An and its preceding relationship between 1 (or first few) can be used to list a formula shown, then the formula is called recurrence formula recurrence formula is a method given sequence.

2. Fibonacci recursion formulas, for example, the number of columns is: An = An-1 + An-2 (n> 2, a1 = 1, a2 = 1)

3. So now if you want expressed recursively Fibonacci number to define the function f (n): When n> 2 when f (n) = f (n-1) + f (n-2); when when n = 1 f (n) = 1, when n = 2 when f (n) = 1;

即private static int fibonacciRe(int i) {
if(i == 1 || i == 2)
return 1;
else if(i>2)
return fibonacciRe(i-1)+fibonacciRe(i-2);
else 
return 0;
}

4. Explain: In fact, plainly recursive function is a recursive formula, as long as the recurrence formula to write a paper, to replace the term A is a function name, replacing n as a function of the parameters can finally handle it with special parameters if the result value when the value is in Europe.

5. Summary: We are hard to understand why some people do recursive function, I personally feel that we are at university was learning computer languages, and at this time of high school mathematics knowledge has been forgotten, and instead we put our brain got stuck in the process of the recursive method calls, for example, be a step by step how to call. If we want to combine the concept of high school mathematics, then it is not difficult to understand recursion, recursive algorithm started to feel a glance to understand that it is my personal previously difficult to understand recursion is because as soon as I saw the hands were under my head in the loop continue down the layers of tune with, find it hard to understand, in fact, we do not think about calling relationships which layers of anti and abstract thought recursive put him out of a recursive formula, so you will suddenly realize feeling, the original recursive so simple. We previously difficult to understand recursion is our own to create their own understanding of its barriers hinder. I think understand this just with the eyes to read three-dimensional paintings have the same purpose on the computer better. Now, if you just give up a piece of paper to write recursive formula, you can immediately write out a recursive function. Ha ha. . Recursion is not very simple single it? ! In fact, our computer algorithms are derived from mathematics, computer algorithm is a good example of the application of mathematics to produce!

6. Attachment: Fibonacci non-recursive algorithm of Number of Recursive a waste of resources, so try not to use less than compelling recursive algorithm

public static int z(int n){
int a=1;
int b=1;
int c = 0;
for(int i=3;i<=n;i++){
c=a+b;
a=b;
b=c;
}
return c;
}


7. These are his own personal meet, there is an error or improper, welcome Tucao!




Guess you like

Origin blog.csdn.net/baidu2030/article/details/41208251