The basic idea of recursive algorithm design is: For a complex problem, the original problem is decomposed into a number of relatively simple and similar sub-problems, continue until the sub-problem is simple enough to be directly solved, that is, to the recursive exit, so that the original problem has a recursive solution.
The key thing to catch is:
(1) Recursive export
(2) The ground pushes towards the exit gradually
Example:
Example: Ask for factorial of 5.
As follows:
Java code
- Public class Test {
- Static int multiply (int n) {
- if (n==1| | n==0)
- return N;
- Else
- return n*multiply (n1);
- }
- Public static void main (string[] args) {
- System.out.println (Multiply (10));
- }
- }
[Java]View Plaincopy
- Public class Test {
- static int multiply (int n) {
- if (n==1| | n==0)
- return n;
- Else
- return n*multiply (n1);
- }
- Public static void Main (string[] args) {
- System.out.println (Multiply (10));
- }
- }
The above multiply is an example of factorial. In fact, recursive recursion, literally, is to invoke its own method in the method itself, or indirectly invoke; Look at the program above, take multiply (5) to say:
n=5; Executive 5*multiply (4);
--------------------
See Multiply (4)
N=4 Executive 4*multiply (3);
-------------------
See Multiply (3)
N=3, Executive 3*multiply (2);
---------------
Mulitply (2);
n=2 executive 2*mulitply (1);
At this time, return 1;
2*1 up back
(2*1) Back up
(2*1) Back up
5* (2*1) = 120
So the program output 120;
This is an example of simple recursion, so it can be seen that the key to recursion is to have a recursive exit (if statement of the ontology), and a recursive method;
The following is I in Baidu know met a friend's question, also about the recursive algorithm:
------------------------Problem------------------------------
I just learned Java, without any programming foundation, you Master laughed at.
Java code
- Public class Count
- {
- static void count (int N) //Recursive method
- {
- if (n<5)
- Count (n+1);
- System.out.print ("+n");
- }
- Public static void main (String args[])
- {
- Count (1);
- System.out.println ();
- }
- }
[Java]View Plaincopy
- Public class Count
- {
- static void count (int n) //Recursive method
- {
- if (n<5)
- Count (n+1);
- System.out.print ("+n");
- }
- public static void Main (String args[])
- {
- Count (1);
- System.out.println ();
- }
- }
Please explain in detail how this program is executed, my understanding is to execute the main function in the Count (1), and then enter the Count method, the N value is 1, so execute the IF statement until Count (5), at this time exit if loop, print n=5, then should not have to execute something, But the answer is 5 4 3 2 1, how is this going, thank you!
--------------------Answer---------------------------
Execute count (1) First, then enter the Count method, and the n value is 1, so execute the IF statement, that is, execute count (2), then enter the Count method, and the n value is 2, so execute the IF statement, that is, execute count (3), and then enter the Count method, the N value is 3, So execute the IF statement, that is, count (4), then enter the Count method, the N value is 4, so execute the IF statement, that is, execute count (5), and then enter the Count method, the N value is 5, so do not execute the IF statement, and then execute System.out.print ("+n"); That is, output 5, then the Count method call for this parameter 5 is ended, return to the Count method that called it the parameter is 4, and then execute System.out.print ("" +n), output 4, and then continue to do so, output 3,2,1. It should be explained here that when Count (5) is executed, COUNT (4), COUNT (3), COUNT (2), COUNT (1) are not executed, and they are waiting for the Count (n+1) in their method body to finish, Then execute the System.out.print ("" +n);
-
Top
-
0
-
Step
Recursive algorithm (RPM)