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 to be solved directly, 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
Examples:
Example: Ask for factorial of 5.
For example, the following:
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 (ten));
- }
- }
public class Test { static int multiply (int n) { if (n==1| | n==0) return n; else return n*multiply (n-1); } public static void Main (string[] args) { System.out.println (multiply);} }
The above multiply is a sample of factorial. In fact recursive recursion, which is literally interpreted as invoking its own method, or indirectly, in the method itself; Look at the program above, take multiply (5) to say:
n=5; run 5*multiply (4);
--------------------
See Multiply (4)
N=4 running 4*multiply (3);
-------------------
See Multiply (3)
N=3, running 3*multiply (2);
---------------
Mulitply (2);
N=2 running 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 a simple recursive example, so it is possible to see that the key to recursion is the recursive exit (if statement of the ontology), and the recursive method;
Below is I in Baidu know met a friend's question, also about the recursive algorithm:
------------------------Problem------------------------------
I just learned Java, no matter what programming basis, 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 ();
- }
- }
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 how this program works, I understand is to run the main function in the Count (1), and then enter the Count method, the N value is 1, so run the IF statement until Count (5), at this time exit if loop, print n=5, and then should not have to run something up, But the answer is 5 4 3 2 1, how is this going, thank you!
--------------------Answer---------------------------
Run count (1) First, then enter the Count method, and the n value is 1, so run the IF statement, that is, run count (2), then enter the Count method, the N value is 2, so run the IF statement, that is, run count (3), and then enter the Count method, the N value is 3, So run the IF statement, that is, run count (4), and then enter the Count method, the N value is 4, so run the IF statement, that is, run count (5), and then enter the Count method, the N value is 5, so do not run the IF statement, and then run System.out.print ("+n"); That is, output 5, and then this time the Count method call of 5 is finished, return to the Count method called it's 4, then run System.out.print ("" +n), output 4, and then continue to do so, output 3,2,1. The point here is that count (4), COUNT (3), COUNT (2), COUNT (1) are not running when you run count (5), and they are all waiting for the Count (n+1) in their method body to finish running. Then run System.out.print ("" +n);
Recursive algorithm for Java