Example analysis of recursive principle in Java _java

Source: Internet
Author: User

The recursive principle in Java is analyzed in this paper. Share to everyone for your reference. The specific analysis is as follows:

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

The programming skill that the program calls itself is called recursion (recursion). Recursion as an algorithm is widely used in programming languages. A procedure or function has a method of calling itself directly or indirectly in its definition or description, it usually transforms a large and complex problem layer into a small scale problem similar to the original problem, and the recursive strategy can describe the multiple computations needed in the process of solving problems with only a small number of programs. Greatly reduces the amount of code in the program. The ability of recursion is to define an infinite set of objects with limited statements.

three conditions for recursion:

Boundary conditions
Recursive forward segment
Recursive return segment
When the boundary condition is not satisfied, recursion advances, and when the boundary condition is satisfied, recursion returns.

The following two sample programs illustrate:

Use Java code to find the factorial of 5. (Factorial =5*4*3*2*1 of 5)

/**
 * Calculates the factorial of 5 (result = 5*4*3*2*1) * @author pplsunny * * * */public
class Test01 {
  public static void Main (string[] args) {
    System.out.println (f (5));
  }
  public static int F (int n) {
    if (1 = n) return
      1;
    else return
      N (n-1);
  }

In this question, according to the recursive three conditions to analyze:

(1) Boundary condition: factorial, multiply to the last number, namely 1, return 1, the program executes in the end;
(2) Recursive forward paragraph: When 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 is 5*4, that is, 5* (5-1), that is, N (n-1)

Using Java code to find a sequence: 1,1,2,3,5,8 ... number 40th digits

/** 
 * Find series: 1,1,2,3,5,8 ... 40th digit 
 * @author pplsunny * */Public  
class Test_fibonacci {  
  public static void Main (string[] args) {  
    System.out.println (f (6));  
  public static int F (int n) {  
    if (1== n | | 2 = n) return   
      1;  
    else return  
      F (n-1) + f (n-2);  
  }  
}  

The breakthrough in this topic: starting from the 3rd digit number, the standard number is the first two digits of the number. To calculate the number of digits in the first digit, you need to calculate the number of digits as a parameter in the method.

(1) First, when the number of digits is 1 and 2 o'clock, the current return value should be 1;

(2) Then, when the number of digits is 3 o'clock, the return value should be =2=1+1;
When the number of digits is 4 o'clock, the return value =3=2+1;
When the number of digits is 5 o'clock, the return value =5=3+2;
When the number of digits is 6 o'clock, the return value =8=5+3;
......
(3) The value of the current number of digits (n) is =f (n-1) +f (n-2) from (2) that is greater than or equal to 3.

Experience: Some beginners may think that recursion is to call themselves, it is not a dead loop. Yes, if the recursive writing is unreasonable, it is a dead loop. However, if the writing is reasonable, plus the "boundary conditions", the program to the end of the time, will be layered back. As we climb the mountain, we climb a layer after layer, and if there is no peak, we will climb up. But if you get to the top of the mountain, just follow the steps on the way up the hill and climb down one layer at a time.

I hope this article will help you with your Java programming.

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.