Question 1388: Step-9

Source: Internet
Author: User
Description:

A frog can jump to level 1 or Level 2 at a time. Find the total number of hops that the frog jumps to an n-level step.

Input:

The input may contain multiple test examples. For each test case,

The input includes an integer N (1 <=n <= 70 ).

Output:

Corresponding to each test case,

Output the total number of hops that the frog jumps to an n-level step.

Sample input:
5
Sample output:
8

Recommendation index :※※

Source: http://ac.jobdu.com/problem.php? PID = 1, 1388

Recursive Formula: F (n) = f (n-1) + f (n-2), N> 2; F (1) = 1, F (2) = 2; Haitao: there are two different options for the first hop: one is to skip level 1 for the first hop. At this time, the number of hops equals to the number of hops for the next n-1 step, that is F (n-1); another option is the first jump level 2, at this time the number of Jump method is equal to the number of the next step of the N-2 level, that is, F (n-2 ). Therefore, F (n) = f (n-1) + (F-2) of different hops at N-level steps ).

Matrix coverage: http://blog.csdn.net/zhu_liangwei/article/details/9979247

Abnormal jump steps: http://blog.csdn.net/zhu_liangwei/article/details/9972557

Hop steps: http://blog.csdn.net/zhu_liangwei/article/details/9972303

Fibonacci series: http://blog.csdn.net/zhu_liangwei/article/details/9971293

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>using namespace std;const int N=71;long long val[N];long long f(const int n){if(n==1)return 1;else if(n==2)return 2;else{if(val[n-1]==0)val[n-1]=f(n-1);if(val[n-2]==0)val[n-2]=f(n-2);val[n]=val[n-1]+val[n-2];return val[n];}}int main(){int n;memset(val,0,sizeof(val));while(scanf("%d",&n)!=EOF){printf("%ld\n",f(n));}return 0;}

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.