Steps-java, steps-java

Source: Internet
Author: User

Steps-java, steps-java
Step jumping Problem

Description:

A frog can jump to level 1 or Level 2 at a time. Find the total number of hops that the frog jumps to an n-level step.

Analysis

In the final analysis, this problem is still a series of fabric. Just look at the Rule carefully. At the beginning, I wrote the results of the first six numbers to find the rule.

Level 1: 1 fib (1) = 1
Level 2: 2 fib (2) = 2
Level 3: 3 fib (3) = fib (1) + fib (2) = 3
Level 4: 5 fib (4) = fib (2) + fib (3) = 5
Five steps: 8 fib (5) = fib (3) + fib (4) = 8
Level 6: 13 fib (6) = fib (4) + fib (5) = 13

Now we can see the law, fib (n) = fib (n-1) + fib (n-2), fib (1) = 1, fib (2) = 2.
The java code is as follows (you can call it directly in the main function ):

public int jumpFloor(int number) {        if (number == 1)            return 1;        else if (number == 2)            return 2;        else             return jumpFloor(number-1)+jumpFloor(number-2);     } 
Abnormal step jumping

Description

A frog can jump to level 1 or Level 2 at a time ...... It can also jump to n levels. Find the total number of hops that the frog jumps to an n-level step.

Analysis

This is also the reasoning method shown above, with no technical knowledge. Write the results of the first six steps. Some of them have been counted above, so we just need to calculate the results of the above two steps. Then calculate

Level 1: 1 f = fib (1) = 1 = 2 ^ 1
Level 2 step: 2 f = fib (2) = 2 = 2 ^ 2
Level 3: 3 f = fib (3) + 1 = 4 = 2 ^ 3
Level 4: 5 f = fib (4) + 3 = 8 = 2 ^ 4
Five steps: 8 f = fib (5) + 8 = 16 = 2 ^ 5
Level 6: 13 f = fib (6) + 19 = 32 = 2 ^ 6

Then the idea is clear, and the code is easy to write. Similar to the above, the knowledge points of recursion and loop are investigated.

public int jumpFloor2(int number) {        if (number ==1)            return 1;        else if(number==2)            return 2;        else            return jumpFloor2(number-1)*2;    }
Summary:

At the beginning, I always had to learn to find some patterns. I first thought about the patterns, found out the patterns, used mathematical methods to solve the problems, and then programmed them. Of course there are many methods, and the implemented programming method can also be a for loop. This should be the most concise code implementation method.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.