Jump Steps __ Jump Steps

Source: Internet
Author: User
Topic description

A frog can jump up to 1 steps at a time, or jump up to level 2. How many jumps are there in total for the frog to jump up the N-level steps?

Analysis:

This is a certain rule, we can analyze

First step: F (1) =1

Two steps: F (2) =2

Three steps: F (3) =3

Level Four steps: F (4) =5

Level five steps: F (5) =8

It can be concluded that from the third, the number equals the first two numbers and f (n) = f (n-1) + f (n-2)

public class Solution {
public int Jumpfloor (int target) {
if (target = = 1) {
return 1;
else if (target = = 2) {
return 2;
} else {
Return Jumpfloor (target-1) + Jumpfloor (target-2);
}
}
public static void Main (string[] args) {
Solution s = new Solution ();
System.out.println (S.jumpfloor (6));
}
}


The second method:

public class Solution1 {
public int Jumpfloor (int target) {
int one = 1;
int two = 2;
if (target = = 1) {
return 1;
}
if (target = = 2) {
return 2;
}
int result = 0;
for (int i = 3;i <= target;i++) {
result = one + two;
one = two;
two = result;
}
return result;
}
public static void Main (string[] args) {
Solution1 s = new Solution1 ();
System.out.println (S.jumpfloor (6));
}
}


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.