Main ideas:By a known condition (known result), a specific relationship is used to progressively recursively (push/reverse) until there is a solution or no solution.
There are two main categories:
Shun-Push,Proceed from the known condition until the solution is introduced.
Inverse Push,Starting from known resultsuntil the solution is introduced.
It is
important to note that each recursive result is a condition for the next step to be pushed.
Shun push:Fibonacci sequencef0=0,f1=1,fn=f (n-1) +f (n-2) (n>=2,n∈n*)
ExampleWhat is the total number of rabbits? a pair of rabbits can have a pair each month, and each pair of rabbits can be born in 3 months. How many rabbits are there in 12 months?
#include <stdio.h> #define NUM 13void Main () {int i; long fid[num]={1,1}; for (i=2;i<num;i++) { fid[i]=fid[ I-1]+FID[I-2]; } for (i=0;i<num;i++) {
Analysis: First, it can be very clear that this is a straight forward problem. The first month 1 pairs, the second month 1 pairs, the third month 2 pairs ... A recursive push. For the problem of push, the main thing is to find the initial condition (1,1,2,3 ... ) and specific relationships (fn=f (n-1) +f (n-2) (n>=2,n∈n*)), find these, and then according to the problem, further refinement can be. program run process analysis slightly.
Reverse Push:
ExampleHow much money should I save? If you want to spend $1000 in the last one months of the university's four-year period, take $1000 per month, and how much you have to save from the start. The bank's interest rate for one year is 1.71%.
#include <stdio.h> #define FETCH 1000/$ 1000 per month # define rate 0.0171//bank's annual interest void Main () {double year[49];//0-48 months int i; year[48]= (double) FETCH; for (i=47;i>0;i--) { year[i]= (year[i+1]+fetch)/(1+RATE/12),} for (i=48;i>0;i--) {
Analysis: We know that the last one months that is 48 months to take the money is 1000 yuan, that 47 months of principal and interest and should be 1000/(1+0.0171/12). The principal and interest of 46 months are reversed by the 47-month principal and interest: (1000/(1+0.0171/12))/ (1+0.0171/12), and by analogy, until January. So this is a typical inverse problem. Starting from the results, the solution is introduced. program run process analysis slightly.
recursive thought is just a simple algorithm to get started, the story is also some very basic things, but the inside of the thinking is worth slowly to understand and figure out. In daily life, we often unconsciously use the idea of recursion. Grasp the essence of the knowledge: from a result, according to the specific conditions, the next result is introduced. Then something complicated will become very simple.
Getting started with algorithms--recursion