Sword refers to the source series of offer-abnormal jumping steps

Source: Internet
Author: User

Topic 1389: Abnormal jumping steps
Time limit: 1 seconds memory limit: 32 trillion special problem: No submit: 1906 resolution: 1102
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.
Input:
The input may contain multiple test samples, for each test case,
The input includes an integer n (1<=n<=50).
Output:
corresponding to each test case,
The output of the frog jumps to an n-level step with a total number of hops.
Sample input:
6
Sample output:

32




Solution Analysis:

Analysis: Using FIB (n) to indicate the frog jumps on the N-Step steps of the number of hops, the frog jumped on the N-Step step of the number of hops 1 (n-Step), set fib (0) = 1;
when n = 1 o'clock, there is only one method of jumping, that is, the 1-Step Jump: Fib (1) = 1;
when n = 2 o'clock, there are two ways of jumping, one-step and second-order jumps: fib (2) = FIB (1) + fib (0) = 2;
when n = 3 o'clock, there are three ways to jump, the first step out of one, followed by FIB (3-1) in the Jump method, the first second out of the second, there is a fib (3-2) in the Jump method, after the first jump out of third order, followed by FIB (3-3) in the Jump method
fib (3) = FIB (2) + fib (1) +fib (0) =4;
when n = N, there is a total of n jumps, the first step out of one order, followed by FIB (n-1) in the Jump method, after the first second out of the second, there is a fib (n-2) in the Jump method ..... ..... After the first step out of the N-order, there is a Fib (n-n) jump in the back.
fib (n) = fib (n-1) +fib (n-2) +fib (n-3) +..........+fib (n-n) =fib (0) +fib (1) +fib (2) +.......+fib (n-1)
and because Fib (n-1) =fib (0) +fib (1) +fib (2) +.......+fib (n-2)
two-type subtraction: Fib (n)-fib (n-1) =fib (n-1) ===== "Fib (n) = 2*fib (n-1)N >= 2
The recursive equation is as follows:


#include <iostream> #include <stdio.h>using namespace std;//recursive long  long f (int n) {    if (n==0) return 1 ;    if (n==1) return 1;    Return 2*f (n-1);} int main () {    int n;    while (scanf ("%d", &n)!=eof) {        printf ("%ld\n", f (n));    }    return 0;}


Sword refers to offer source series-Abnormal jumping steps

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.