The Phantom 2016Java Internet Direction One of the questions--frog jumping steps

Source: Internet
Author: User
Problem Description: A frog can jump up to 1 steps at a time, can also jump up to level 2 ... It can also jump up n levels. How many jumps are there in total for the frog to jump up the N-level steps?

Topic Analysis:

1 here the F (n) represents the n steps there is a 1,2,... N-order number of hops.

2 n = 1 o'clock, only 1 kinds of jumps, f (1) = 1

3 N = 2 o'clock, there will be two jumps, a 1-order or 2-order, which returns to the problem (1), f (2) = f (2-1) + f (2-2)

4 n = 3 o'clock, there will be three ways to jump, 1, 2, 3,

So that is the first time to jump out of the 1-step after the rest: F (3-1); first jump out of 2, leaving the F (3-2), the first 3 order, then the rest F (3-3)

So the conclusion is F (3) = f (3-1) +f (3-2) +f (3-3)

5) n = n, there will be N in the way, 1-step, 2-order ... n-order, come to the conclusion:

F (n) = f (n-1) +f (n-2) +...+f (n (n-1)) + f (n-n) => F (0) + F (1) + F (2) + F (3) + ... + f (n-1)

6 The above is already a conclusion, but for simplicity we can continue to simplify:

F (n-1) = f (0) + F (1) +f (2) +f (3) + ... + f ((n-1)-1) = f (0) + F (1) + F (2) + F (3) + ... + f (n-2)

F (n) = f (0) + F (1) + F (2) + F (3) + ... + f (n-2) + f (n-1) = f (n-1) + f (n-1)

Can be drawn:

F (n) = 2*f (n-1)

7 The final conclusion, in the N-step, once there are 1, 2 、... N-order Jump mode, the total jump method is:

| 1, (n=0)

F (n) = | 1, (n=1) | 2*f (n-1), (n>=2)

public class Solutionn {
public static int Jumpfloor (int target) {
if (target <= 0) {
return-1;
} else
if (target = = 1) {
return 1;
} else {
Return 2 * Jumpfloor (TARGET-1);
}
}

public static void Main (string[] args) {
System.out.println (Jumpfloor (3));
}
}

Simple summary:

Because of n steps, the first step has n kinds of jump method: Jump 1, jump 2, to jump N-Class
Jump Level 1, left n-1 level, then the rest of the jump is F (n-1)
Jump Level 2, left n-2 level, then the rest of the jump is F (n-2)
So f (n) =f (n-1) +f (n-2) +...+f (1)
Since f (n-1) =f (n-2) +f (n-3) +...+f (1)
So f (n) =2*f (n-1)

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.