Jumping Steps Problem-java

Source: Internet
Author: User

Jumping Step problem

Title Description:

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.

Analytical

This question in the final analysis is a fee BRAC series, carefully find the law can, just start to do when I was directly write the first six number of results to find the law.

First steps: 1 Kinds of fib (1) =1
Two steps: 2 Kinds of fib (2) =2
Three steps: 3 Kinds of FIB (3) =FIB (1) +fib (2) =3
Step four: 5 fib (4) =FIB (2) +FIB (3) =5
Step Five: 8 fib (5) =FIB (3) +FIB (4) =8
Step Six: 13 fib (6) =fib (4) +FIB (5) =13

Now you see the rules, FIB (n) =fib (n-1) +fib (n-2), fib (1) =1,fib (2) = 2.
The Java code looks like this (called directly in the main function):

publicintjumpFloor(int number) {        if1)            return1;        elseif2)            return2;        else             return jumpFloor(number-1)+jumpFloor(number-2);     
The problem of abnormal jumping steps

Title Description

A frog can jump up to 1 steps at a time, or jump up to level 2 ... It can also jump on n levels. Ask the frog to jump on an n-level step with a total number of hops.

Analytical

This is also the reasoning method shown above, there is no technical content. Write the results of the first six jump steps, this part of the above has been counted out, so we just can jump 2 steps above the result on the line. And then figure out

First steps: 1 Kinds of F=FIB (1) =1=2^1
Two steps: 2 Kinds of F=FIB (2) =2=2^2
Three steps: 3 Kinds of F=FIB (3) +1=4=2^3
Four steps: 5 Kinds of F=FIB (4) +3=8=2^4
Five steps: 8 Kinds of F=FIB (5) +8=16=2^5
Six steps: 13 Kinds of F=FIB (6) +19=32=2^6

Then this idea is clear, the code is good to write, and the above is similar, is to examine the recursive and circular knowledge points.

publicintjumpFloor2(int number) {        if (number ==1)            return1;        elseif(number==2)            return2;        else            return jumpFloor2(number-1)*2;    }
Summarize:

When doing a problem, just start to learn to find some rules, first think good ideas, find out the law, the problem with mathematical methods to find ways to solve the law, and then use programming to achieve. Of course there are many ways, the implementation of the programming method can also be for the loop, this should be the most concise code implementation method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Jumping Steps Problem-java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.