-
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;}