The climbing steps of the algorithm

Source: Internet
Author: User

N-level stairs, one can be 1 or 2, ask how many different ways to go?

Solution:

If n represents the progression of a step, a n indicates that when a person walks to the nth step, all possible different methods are readily available:
(1) According to test instructions:

When N=1, it is obvious that only 1 kinds of cross-laws, namely a1=1.
When the n=2, you can step to the level of the cross, you can also step across level two,
Therefore, there are 2 different kinds of cross-laws, namely a2=2.
(2) by (1) Available:
If the first step crosses one level, then there are two steps left, and there are a2=2 (kinds) of cross-laws.
If the first step crosses two steps, then there is still a level of steps, there is a1=1 (kind) cross-law.
Therefore, a3=a1+a2=3
(3) When n=4, there are three kinds of situations discussed separately:
If the first step crosses one level, then there are three steps left, and there are a3=3 (kinds) of cross-laws.
If the first step crosses two steps, then there are two steps left, and there are a2=2 (kinds) of cross-laws.

So a4=a3+a2=5

(4) When n=5, there are 4 kinds of situations discussed separately:
If the first step crosses one level, then there are four steps left, and there are a4=5 (kinds) of cross-laws.
If the first step crosses two steps, then there are three steps left, and there are a3=3 (kinds) of cross-laws.
So a5=a3+a4=8

int func (int  n) {    if (1 = = n        )return 1;     Else if (2 = = n        ) return 2;
return func (n-1) + func (n-2);}

The climbing steps of the algorithm

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.