Summary of recursive algorithms in Java programming
1. What is recursion
Personal understanding is to call yourself until a condition is met to end the process. This is recursion. Take a popular example: if you are in a cinema, you want to know which row you are sitting in, but there are many people in front of you, so you don't bother to count, so you ask the people in the front row, "which row are you sitting in?", In this way, after the person in front of you (code a) answers you, you will know which row you are in - as long as you add one to the answer of a, it will be your row. Unexpectedly, a is lazier than you, and he doesn't want to count, so he also asks person B in front of him, "which row do you sit in?", A can use as like as two peas of your own layout. Then B did the same until they asked the front row (or asked the person who knew which row he was, indicating the end of the call). The person in the first row told the person who asked the question "I'm in the first row". Finally, everyone knew which row he was in
2. The basic idea of recursive algorithm design is:
For a complex problem, decompose the original problem into several relatively simple and similar subproblems, and continue until the subproblem is simple enough to be solved directly, that is, when it comes to the exit of recursion, so that the original problem can be solved recursively.
The key is:
(1) Recursive exit (2) is pushed gradually towards the exit
3. Common recursive algorithms
(1) The most common is factorial. For example, to find the factorial of 5, the mathematical formula is: 5 * 4 * 3 * 2 * 1, code:
(2) Find the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7... + 1000
(3) 1,1,2,3,5,8,13,21,34... Find the 30th number by recursion
Recursive implementation of 99 multiplication table
The code is as follows:
Code analysis of printing 99 division table with Java
The above is the summary of the recursive algorithm of Java programming in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: code examples of various sorting algorithms implemented in Java, code examples of heap sorting of Java algorithm, detailed explanation of examples of calculating the approximate value of PI by Java Monte Carlo algorithm, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!