C language: A frog can jump up to 1 steps at a time, you can also jump on level 2. How many hops does the frog have to jump on an n-level stair altogether?

Source: Internet
Author: User

A frog can jump up to 1 steps at a time , or jump up to level 2 . How many hops does the frog have to jump on an n-level stair altogether?

Solution: PutNthe jumping method of the step isf (N), whenn>2, there are two different choices when jumping for the first time: first, jumping only1the number of hops equals the rest of the back.n-1the number of jumps of a step, that isf (n-1);the other is the first jump .2the number of hops equals the rest of the back.n-2the number of jumps of a step, that isf (n-2);soNthe jumping method of the step isf (N) =f (n-1) +f (n-2). It is not hard to see that this is actually a variant application of the Fibonacci sequence, which moves each of the Fibonacci sequence forward.1bit.

Program:

#include <stdio.h>


int Fibonacci (int n)

{

int NUM1 = 1, num2 = 1, num3 = 0, i = 0;

if (n <= 1)

{

return NUM1;

}

for (i = 1; i < n; i++)

{

num3 = Num1 + num2;

NUM1 = num2;

num2 = num3;

}

return num3;

}


int main ()

{

int num = 0, ret = 0;

printf ("Please enter the number of steps:");

scanf ("%d", &num);

ret = Fibonacci (num);

printf ("A total of%d hops!") \ n ", ret);

return 0;

}


Results:

Please enter the number of steps : 3

There are 3 Different kinds of jumping methods!

Please press any key to continue ...

expansion:   Span style= "FONT-FAMILY:CALIBRI;" >1 steps can also jump on 2 level, n class. Ask the frog to jump to the previous n level steps how many kinds of jumping?

Solution: We can obtain a total of f (n) =2^ (n-1) by mathematical induction method

Program:

#include <stdio.h>

#include <math.h>


int Fibonacci (int n)

{

Return pow (2,n-1);

}


int main ()

{

int num = 0, ret = 0;

printf ("Please enter the number of steps:");

scanf ("%d", &num);

ret = Fibonacci (num);

printf ("A total of%d hops!") \ n ", ret);

return 0;

}

Results:

Please enter the number of steps : 3

There are 4 Different kinds of jumping methods!

Please press any key to continue ...


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1742286

C: A frog can jump up to 1 steps at a time, or jump up to level 2. How many hops does the frog have to jump on an n-level stair altogether?

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.