Analysis of recursive principles in Java

Source: Internet
Author: User

Explanation: The Program Calling technique is called recursion. The Programming Technique of program calling itself is called recursion ). Recursive Algorithms are widely used in programming languages. A process or function has a method that calls itself directly or indirectly in its definition or description, it usually converts a large and complex problem into a small problem similar to the original problem to solve it, the recursive strategy can describe the repeated computing required for the problem-solving process with only a small number of programs, greatly reducing the amount of code in the program. The ability to recursion lies in the use of limited statements to define an infinite set of objects. Three conditions of recursion: boundary condition recursion forward Segment recursion return segment when the boundary condition is not met, recursion advances; when the boundary condition is met, recursion returns. The following two sample programs are used to describe the factorial of 5 using Java code. (5 factorial = 5*4*3*2*1) [java] package org. wxp. recursion;/*** calculate the 5 factorial (result = 5*4*3*2*1) * @ author Champion. wong ***/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, we will analyze the three recursive conditions: (1) boundary condition: factorial, multiplied to the last number, that is, 1, returns 1, and the program is executed to the end; (2) recursive forward segment: when the current parameter is not equal to 1, call itself. (3) recursive return segment: multiply from the maximum number. If the current parameter is 5, 5*4, that is, 5 * (5-1), that is, n * (n-1) uses Java code to calculate the sequence:, 5, 8 ...... the number of 40th bits [java] package org. wxp. recursion;/*** calculates the sequence: 1, 1, 2, 3, 5, 8 ...... the number of 40th bits * @ author Champion. wong **/public class Test_02_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 question: Starting from 3rd digits, the current digit is the first two digits and. To calculate the maximum number of BITs, the number of BITs must be passed as a parameter for calculation. (1) first, when the number of digits is 1 and 2, the current returned value should be 1; (2) then, when the number of digits is 3, the return value should be = 2 = 1 + 1; when the number of digits is 4, the return value is 3 = 2 + 1. When the number of digits is 5, the return value is 5 = 3 + 2. When the number of digits is 6, return Value = 8 = 5 + 3 ;...... (3) from (2) know, greater than or equal to 3, the current number of digits (n) = f (n-1) + f (n-2) Experience: some beginners may think that recursion is self-called, so it is not an endless loop. Right, if recursive writing is unreasonable, it is an endless loop. However, if the write is reasonable and the "boundary condition" is added, the program will return layer by layer when it is executed. Just like we climb a mountain, we climb another layer around the mountain road. If there is no mountain, we will continue to climb up. However, if you reach the top of the hill, you will climb down one layer by one as you go up the hill.

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.