25 steps, each step or two, ask the maximum number of ways to go

Source: Internet
Author: User

Analysis:
There are a total of 25 steps, each step has two kinds of steps, go first level, or go two level. In two cases: if the first time to walk two, then there are
25-2=23 Ladder to go. And then ask for the remaining 23 steps to the total number of walking method. If the first step, then there are 25-1 = 24 steps
To go, so the total number of steps after the 25 ladder, is equal to climb the 23 steps Total method + climb up the 24 steps of the total number of methods. And the 23 pole can
Then divided into (23-1), (23-2) level ladder. By analogy, this is a typical recursive type. We can easily figure out when
There are 1 and 2 steps when all the Times: 1 and 2 respectively. The function of the total number of methods is calculated as follows:

---------------------------------Getnum ()---------------------------------------------
public int getnum (int i)
{
int r=0;
if (i>2)
{
Num=getnum (i-1) +getnum (i-2);
return num;
}
else if (i==1)
{
R=1;
}
else if (i==2)
{
r=2;
}
return R;
}
---------------------------------------------------------------------------------------
The test program is written in Java. Run using command: Java stair 25

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.