Go (Java)

Source: Internet
Author: User

Explanation: The programming skill called by the program itself is called recursion.

The programming technique called by the program itself is called recursion (recursion). Recursion as an algorithm is widely used in programming language. A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements.   Recursive three conditions: Boundary condition recursive forward segment recursive return segment when the boundary condition is not satisfied, recursive forward; when the boundary condition is satisfied, the recursion returns.   Below is illustrated by two sample programs: Use Java code to find the factorial of 5. (5 of factorial =5*4*3*2*1)  [java]  package org.wxp.recursion;    /**  * calculates the factorial of 5 (result = 5*4*3*2*1)   * @author champion.wong  *    *  */ public class Test01 {     public static void main (string[] args) { &nbsp ;       SYSTEM.OUT.PRINTLN (f (5));                 public static int F (int n) {     &nbsp ;   if (1 = = N)               return 1;          Else               return n (n-1);     }  }     in this topic, follow the recursiveThree conditions to analyze: (1) boundary condition: factorial, multiply to the last number, that is, 1, return 1, program execution in the end; (2) Recursive forward segment: The current parameter is not equal to 1, continue to call itself, (3) Recursive return segment: From the maximum number of times, if the current parameter is 5, then it is 5*4, that is, 5 * (5-1), i.e. n (n-1)   Use Java code to calculate the number of 1,1,2,3,5,8 ... 40th digit [Java]  package org.wxp.recursion;    /**  *: 1,1,2,3,5,8. Number of 40th digits   * @author champion.wong  *   */ public class Test_02_fibonacci {     public static void main (string[] args) { & nbsp       SYSTEM.OUT.PRINTLN (f (6));                 public static int F (int n) {     &nbs P   if (1== n | | 2 = = n)               return 1;          Else              return F (n-1) + f (n-2);       }     the breakthrough in this topic is: Starting with the 3rd digit, the standard number is the first two digits. To calculate the value of the number of bits, you need to calculate the number of bits as a parameter passing in the method. (1) First, when the number of bits is 1 and 2 o'clock, the value that is currently returned should be 1; (2) Then, when the number of bits is 3 o'clock, the return value should be =2=1+1;      &NBsp           4 o'clock, return value =3=2+1;                  When the number of bits is 5 o'clock, return Values =5=3+2;                  When the number of digits is 6 o'clock, the return value =8=5+3;                &nbsp, ..... (3) from (2) learned that, greater than or equal to 3 of the case, the current number of digits (n) of the numerical =f (n-1) +f (n-2)      experience: Some beginners may think that recursion is to call themselves, it is not a cycle of death. Yes, if the recursive writing is unreasonable, that is the cycle of death. However, if the writing is reasonable, plus "boundary conditions", when the program executes in the end, it will return by layer. As we climb the mountain, we climb one layer after another, and if there is no peak, we will climb up. But if you reach the top of the mountain, follow the steps of the mountain to climb down one layer at a time.  

Go (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.