15. recursive

Recursive thinking:
  1. A method call B method, it is easy to understand!
  2. Recursion is: A method call A method! That calls itself
  3. Recursion can use a simple program to solve complex problems. It is usually a big problem with the original layers into a problem similar but smaller problem solving, recursive strategy requires only a small number of procedures can describe the problem-solving process is repeated computing needs, greatly reduced the amount of code of the program. Recursive infinite set of capabilities that with limited statement to define object.
  4. Recursive structure consists of two parts:
Recursive body: when not to call their own methods. If there is no head, into an infinite loop.
Recursive head: When do I need to call their own methods.

1  Package Penalty for com.duan.method;
 2  
3  public  class Demo06 {
 4      // recursive thought: You can use a small base, if you call too much themselves, but will affect machine performance. This algorithm is good enough 
. 5      public  static  void main (String [] args) {
 . 6  
. 7          System.out.println (F (. 5)); // 120 
. 8          System.out.println (F (. 4)); // 24 
. 9      }
 10  
. 11      @ factorial 5! . 4. 3 * * *. 5 * 2. 1 
12 is      public  static  int F ( int n-) {
 13 is          IF (n-==. 1 ) {
 14             return 1;
15         } else {
16             return n * f(n - 1);
17         }
18     }
19 }
Recursive would not be able to do recursive, provided that the use of the base is relatively small, because the deeper, more difficult for the computer, although convenient for programmers, recursion can all be replaced by other methods. Java is in the stack mechanism is a method to the bottom of a pressure (main () method advanced stack). Once the base is too large, easy to memory overflow.


Guess you like

Origin www.cnblogs.com/duanfu/p/12222316.html