OFFER prove safety of metamorphosis jump stairs (nine degrees OJ1389)

Subject description:

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

 

Input:

Input may contain multiple test samples for each test case,

Input comprises an integer n (1 <= n <= 50).

 

Output:

Corresponding to each test case,

The output of the frog jumped into a total of n grade level how many jumps.

 

Sample input:

6

 

Sample output:

32

Problem-solving ideas:

  This question is similar steps before the jump with only the order of the hop step change from 1 to n, that is to say, is no longer a problem jump or jump twice, but jumped issues under n. So apparently problem-solving ideas have reverse analysis, we found that:

  Each step can be the final step to jump, you can jump from his previous steps it up, you can also jump two steps up from his first two steps. Then summarize found:

  Finally, the remaining number of steps, coupled with previous methods hop steps , you can. which is:

  The last remaining zero level, for the time being set to zero, jump directly to the n-th step up, obviously only one way, every time we add a loop from first on the list.

  The last remaining step 1, the total of (the number of the n-1 first method step) species;

  Finally, the remaining two steps, a total of (n-2 number of steps of the method) species;

  ....

  Finally, the n-1 remaining steps, only one way.

  Add up the above method, both the number of jumps to step n-th order .

Code:

 

#include <stdio.h>
long long int arr[51] = {0,1};
void createArr(void);
int main(void){
    int n;
    createArr();
    while(scanf("%d",&n)!=EOF && n>=1 && n<=50){
        printf("%lld\n",arr[n]);
    }
    return 0;
}
void createArr(void){
    int i,j;
    for(i=2;i<51;i++){
        j=i-1;
        arr[i]++;//直接跳跃到本身的
        while(j){
            arr[i] += arr[j];
            j--;
        }
    }
}

 

 

 

Reproduced in: https: //my.oschina.net/u/204616/blog/544989

Guess you like

Origin blog.csdn.net/weixin_33911824/article/details/91989790