A programming question about the breeding of rabbits:
There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, the small rabbit to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?
I think for a long time, the idea of a problem, and even the laws are not clear. after looking at some of the algorithms on the web, I found a thought that was not used before: Recursion. the current understanding of recursion is limited to the beginner Level.
This programming question should be considered in this way:
What is the origin of the Rabbits each month? a: The number of rabbits last month (whether or not they have the ability to Reproduce) + 2 months ago the number of rabbits in the 1th and 2 months, only the first pair of Rabbits.
If you think about it, you can get the number of rabbits this month by using the number of rabbits in the last month + the number of rabbits last month. The result is a well-known Fibonacci sequence.
1 public Static intMethodintMonth) {2 if(month = = 1 | | month = = 2){3 return1;4}Else{5 intnum = Method (month-1) + method (month-2);6 returnnum;7 }8}
Thought is result = result of last program + result of last program
After viewing the information about recursion on the internet, I saw two other problems solved by recursive thinking: stair climbing and Hanoi Tower.
Stair climbing problem
Suppose that a staircase has an N-step stair, and that the person can cross the M-order up to a total number of stair-climbing schemes at a Time.
Analysis:
Number of steps (walk METHOD)
1 1 1
2 11 2 2
3 111 12) 21 3
4 1111 112 121 211 225
therefore, This problem and the previous breeding problem of rabbits is a truth.
1 public Static intFintN) {2 if(n = = 0) {3 return0;4}Else if(n = = 1) {5 return1;6}Else if(n = = 2) {7 return2;8}Else {9 Ten returnF (n-1) + F (n-2); one } a}
Hanoi Tower Problem:
From left to right A B C pillar Large plate under the small plate on the, with a b-pillar to move all the plates from the A-column to the C-pillar, the big Plate only under the small Plate.
I think of these problems more difficult, but the online analysis of a lot, almost covered all my considerations, therefore, I am here only to say the following is a more simple and quick way of understanding.
A,b,c three needles, false with n plates, only need to be divided into three steps, ① will (n-1) a plate from a to b,② the largest plate from a to C,③ will (n-1) a plate from B to C
F (n) = (f (n-1) * *) + 1;
public Static int method (int n) { if(n = = 1) {return 0; } Else if (n = = 2) {return 1; } Else { return Method (n-1) * + 1; } }
For example, use recursive thinking to print the 9*9 multiplication table
Normal if you do not use iterations, you can implement code like this, using a two-layer nested for loop
1 public Static voidmethod_1 () {2 for(inti = 1;i <= 9;i++){3 for(intj = 1;j <= i;j++){4System.out.print (i + "*" + j + "=" + i*j + "");5 }6 System.out.println ();7 }8}
If you use recursion, the problem becomes: print the last result and print a new line
1 public Static voidMethod_2 (intI) {2 if(i = = 1) { 3System.out.println ("1*1=1"); 4}Else { 5Method_2 (i-1); 6 for(intj = 1; J <= i; J + +) { 7System.out.print (j + "*" + i + "=" + J * i + ""); 8 } 9 System.out.println ();Ten } one}
For recursive applications, in general, it is necessary to find and find recursion in the program to simplify the Problem.
Recursive algorithm in initial contact with Java