1. Understanding of the recursive function of understanding, I think it is more important, because many great gods can use recursive functions of the lifelike, not only their programming skills, but also mainly to understand the algorithm. The straightforward understanding is that if the logic of an event can be expressed in the form of f (x) =nf (x-1) +o (x), then it can be achieved by recursive thinking.
When writing recursive logic, you should know the following rules: 1. To have a benchmark, for example, f (x) =f (x-1) +1, if the value of f (0) is not added to the benchmark, then the function will execute indefinitely without meaning 2. Continuous propulsion is f (x) =f (x-1) or F (x) =f (x/n) Yes, of course. Each recursive function has a more magical step, which is the backtracking step, for example: Fact ( 3)-----Fact (2)-----Fact (1)------fact (2)-----Fact (3)------------------------------>------------------------------>
Recursive backtracking
2. Example implementations
calculation and f (x) = 0, first list the formula F (x) =f (x-1) +x/(4**x) (Two * * represents the second party, Python is used to), get the following code
public class Recursion {public static void Main (String args[]) { System.out.print (f (2)); } public static double F (int x) { if (x==0) { return 0; } Else{ return F (x-1) +x/math.pow (4,x);}}
The result is: f (2) = 0.375, verify correct
3. Calculation of time complexity
The above is an example of the expansion of F (x) =f (x-1) +x/(4**x),
Subtract f (x) by 4, so that the x side of 4 is equal to K, then the primitive time complexity, and log is the base of 4.
Reference: http://blog.csdn.net/budapest/article/details/6367973
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
Simple analysis of recursion of "Java implementation of algorithm data structure" and computation of time complexity