The step problem of recursion

Source: Internet
Author: User

Jumping Step 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.

Thinking of solving problems

When N=1, the number of times f (n) = 1.
When n=2, the number of times f (n) = 2. (11 or 2)
When n>2, the current step can jump one level, you can also jump two levels, the number of f (n) =f (n-1) +f (n-2).

Implementation code
class Solution {public:    number) {        if (number2)            returnnumber;        else            return jumpFloor(number1) + jumpFloor(number2);    }};
Abnormal jumping steps

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.

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.

Thinking of solving problems

When the number of steps is n, it can be divided into the following steps to complete:
The number of steps for the first hop is S, and the number of steps to skip is T, then:
(1) S=1, t (n) = t (n-1)
(2) s=2, t (n) = t (n-2)
.
.
.
(n) s=n, t (n) = t (0) = 1
So the total number of steps to skip can be expressed as:
T (n) = t (0) + t (1) + t (2) + ... + t (n-1)
Because T (0) = t (1) = 1, so t (n) = 2^ (n-1)

Implementation code
class Solution {public:    intjumpFloorII(int number) {        return pow(21);    }};

The step problem of recursion

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.