The study of Recursive algorithm
The algorithm that can use recursive description usually has this characteristic: in order to solve the problem of scale N, we try to decompose it into smaller problem, then we can construct the solution of big problem conveniently from the solution of these small problems, and these smaller problems could be decomposed into smaller problems by using the same decomposition and synthesis method. And the solution of the larger problem is constructed from the solution of these smaller problems. In particular, when scale is n=1, it can be solved directly.
Example: Little monkey eats JuJube
The first day of the monkey picked a number of dates, immediately ate half, not enjoyable and ate one more, the next day ate the remaining half and ate one more;
After eating every day, the last half of the rest of the one more. To the tenth day the monkey again want to eat, see only a dates left. Q. How many dates are there on the first day?
From the above question we can see a pleasing rule, the day is 1, the nineth to the first day after the day with 1 and twice times the same as the previous one.
The following is a description of this rule: (Complete program please download)
public int Taozi (int x) {
if (x>=10) {
return 1;
}
else{
Return (Taozi (x+1) +1);
}
}
When we define the Taozi () function, it is defined by the Taozi () itself, which is recursion. Recursion is a special loop, a loop with a very nice loop rule. In the title we just have to Taozi (1), that is, the first day to print out, all OK. And this is how the middle of the work, we can ignore.
Problem combination Problem
Problem Description: Find all combinations of r numbers from the natural number 1, 2 、......、 N.
For example, all combinations of n=5,r=3 are:
(1) 5, 4, 3 (2) 5, 4, 2 (3) 5, 4, 1
(4) 5, 3, 2 (5) 5, 3, 1 (6) 5, 2, 1
(7) 4, 3, 2 (8) 4, 3, 1 (9) 4, 2, 1
( 10) 3, 2, 1
Analysis of the 10 combinations listed, you can use such recursive thinking to consider the algorithm of combinatorial functions. Set the function to public void comb (int m,int k) to find all combinations of the number of k from the natural number 1, 2 、......、 m. When the first number of the combination is selected, the subsequent number is the combination of the number of k-1 from the remaining number of m-1.
This will transform the combinatorial problem of the number of K in M number into the combinatorial problem of finding the number of k-1 in M-1 number.
set function into working array a[] holds the number of the calculated combination, and the contract function places the first number of the identified K-number combinations in a[k], and when a combination is found, a combination of a[] is output.
The first number can be M, m-1 、......、 K, and the function will determine that the first number of the combination is placed in the array, there are two possible choices, because the remaining elements of the group have not been determined, continue to pass the return determination, or because all the elements of the combination have been determined, Output this combination. See the following procedure for details:
public class combtest{static int a[]=new int[100]; static void comb (int m,int k) { int i,j; for (i=m;i>=k;i--) { a[k]=i; if (k>1) comb (i-1,k-1); else { for (j=a[0];j>0;j--) System.out.printf ("%4d", A[j]); System.out.println (); } } } public static void Main (String args[]) { a[0]=3; Comb (5,3); a[0]=4; Comb (10,4); } }
Run:
C:\java>java Combtest
5 4 3
5 4 2
5 4 1
5 3 2
5 3 1
5 2 1
4 3 2
4 3 1
4 2 1
3 2 1
The study of Recursive algorithm