Problem
A frog can jump up to 1 steps at a time, or jump up to level 2. Ask the frog to jump on an n-level step with a total number of hops.
Ideas
When N=1, there is only one method of jumping, and f (1) = 1, when n=2, there are two kinds of jumping, and f (2) = 2, when n=3, you can jump from the n=1 directly to the n=3, can also jump from n=2 directly to N=3, and F (3) =f (1) +f (2) =3 ..., so you can use recursion, top-down , one-step solution, but careful analysis, if n=10, F (9) and F (8) are required, and F (9) =f (8) +f (7), F (8) =f (7) +f (6), it is apparent that the repetition of F (8) and F (7) is obtained, and as the n increases, the complexity increases exponentially.
Packageoffer009;ImportJava.util.Scanner;ImportJava.util.concurrent.TimeUnit;/*** @Title: Main.java * @Package: offer009 * @Description frog jumping steps, recursive implementation (large calculation) *@authorHan * @date 2016-4-18 pm 1:24:17 *@versionV1.0*/ Public classMain { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); intn = 0; while(Scanner.hasnext ()) {n=Scanner.nextint (); //nanosecond at the beginning LongStart =System.nanotime (); LongSteps =getsteps (n); //the number of nanoseconds spent LongDuring = System.nanotime ()-start; //convert nanoseconds to milliseconds Longseconds =TimeUnit.MILLISECONDS.convert (during, timeunit.nanoseconds); System.out.println (steps); System.out.println ("When n is" + n + "It takes time to" + seconds + "milliseconds"); } } Private Static LongGetsteps (intN) {if(N < 1) Throw NewRuntimeException ("Error Input"); if(n = = 1) return1; Else if(n = = 2) return2; Else returnGetsteps (n-1) + getsteps (n-2); }}
Test
When you n>40, you'll find that the calculations are starting to slow down.
Ideas
Another solution, the sub-bottom upward method, similar to the dynamic programming, the results of this time is the use of the next calculation, a large number of reduction in calculation times.
PackageOffer009other;ImportJava.util.Scanner;ImportJava.util.concurrent.TimeUnit;/*** @Title: Main.java * @Package: Offer009other * @Description frog jumping steps, dynamic planning implementation (large computational capacity) *@authorHan * @date 2016-4-18 pm 1:38:17 *@versionV1.0*/ Public classMain { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); intn = 0; while(Scanner.hasnext ()) {n=Scanner.nextint (); //nanosecond at the beginning LongStart =System.nanotime (); LongSteps =getsteps (n); //the number of nanoseconds spent LongDuring = System.nanotime ()-start; //convert nanoseconds to milliseconds Longseconds =TimeUnit.MILLISECONDS.convert (during, timeunit.nanoseconds); System.out.println (steps); System.out.println ("When n is" + n + "It takes time to" + seconds + "milliseconds"); } } Private Static LongGetsteps (intN) {LongForgstepminusone = 2; LongForgstepminustwo = 1; LongSteps = 0; for(inti = 3; I <= N; i++){ //The current N-Step some jump methodSteps = Forgstepminusone +Forgstepminustwo; //for the next calculation to prepare, this is the n+1-2 step of the number of jumping methodForgstepminustwo =Forgstepminusone; //for the next calculation to prepare, this is the n+1-1 step of the number of jumping methodForgstepminusone =steps; } returnsteps; }}
Test
Summarize
Recursive implementation of the Fibonacci sequence is understandable, but there are too many meaningless calculations.
Frog Jumping Steps (Fibonacci series)