Monkey mountain climbing and hierarchical recurrence

Source: Internet
Author: User

I. Special Cases

 

1. Question proposal

A stubborn monkey climbs a mountain and jumps on a hill with 30 steps. You can jump up to level 1 or level 3 in one step, and find out how many ways to jump up the hill.

2. Solutions

Which level is it located before it reaches level 30th ?? There are two situations: Level 1, level 1, level 1 arrival, level 2, level 3 arrival.

So there are:

F (30) = f (29) + f (27)

There is a recurrence relationship in general:

F (K) = f (k-1) + f (k-3)

The initial condition is:

F (1) = 1

F (2) = 1 (1 + 1)

F (3) = 2 (1 + 1 + 1, 3)

 

3. program code

// The monkey climbs the mountain for N levels, one step across level 1 or level 3 <br/> # include <stdio. h> </P> <p> int main (void) <br/> {<br/> int K, N; <br/> long f [1000]; // F [I] storage of the total number of hop I-level steps </P> <p> printf ("Enter the total number of steps N :"); <br/> scanf ("% d", & N); <br/> F [1] = 1; F [2] = 1; F [3] = 2; <br/> for (k = 4; k <= N; k ++) <br/> F [k] = f [k-1] + F [k-3]; <br/> printf ("% d hops/N", N, F [N]); </P> <p> return 0; <br/>}

 

II. General situation

1. in-depth questions

Generalize the parameters of the problem. There are N levels of the climbing steps and the same hop method in one step. The numbers of N and m are keyboard input.

2. Hierarchical Recursive Algorithm Design

Set the different crawling methods for the t-level step up to f (t), and set the number of M Integers to X (1) for each step input from the keyboard ), X (2 ),..., X (m) (Convention x (1) <X (2) <... <X (m )).

Because the number of steps X (I) that one step can span is input by the keyboard, we do not know it beforehand. Therefore, we cannot simply determine f (x (1) during design )), f (x (2 )),....

In fact, the initial conditions can be obtained in hierarchical recursion, and multiple link groups can be used for recursion.

 

First, we will discuss the recursive relationship of f (t:

When T <X (1), F (t) = 0; f (x (1) = 1 (initial condition );

When x (1) <t <= x (2), level 1 recursion: F (t) = f (t-x (1 ));

When X (2) <t <= x (3), level 1 recursion: F (t) = f (t-x (1 )) + f (t-X (2 ));

......

Generally, when x (k) <t <= x (k + 1), k = 1, 2,... m-1, There is a level K recursion:

F (t) = f (t-x (1) + f (t-X (2) +... + f (t-X (k ))

When x (m) <t, the M-level recursion:

F (t) = f (t-x (1) + f (t-X (2) +... + f (t-X (m ))

When T = x (2), or T = x (3 ),..., or T = x (M), calculate F (t) Based on the above recursion, and Add 1. The principle is very simple, so t itself is a one-step crawling method. To this end, we should add:

F (t) = f (t) + 1 (t = x (2), X (3),..., x (m ))

The goals are as follows:

F (n) = f (n-x (1) + f (n-X (2) +... + f (n-X (m ))

 

Special Handling

In the recursive design, we can record N as an array element x (m + 1). This processing is clever and can be performed based on the same recursive law to simplify the algorithm design, the last f (x (m + 1) is F (n ).

When F (n) is output, the additional 1 must be subtracted.

 

2. Code Implementation

# Include <stdio. h> </P> <p> int main (void) <br/> {<br/> int I, J, K, M, N, X [10]; <br/> long f [200]; </P> <p> printf ("Enter the total number of steps:"); <br/> scanf ("% d ", & N); <br/> printf ("several hops at a time:"); <br/> scanf ("% d", & M ); <br/> printf ("step by step from small to large); <br/> for (I = 1; I <= m; I ++) // input m Step-by-Step Jump series <br/>{< br/> printf ("Step % d:", I ); <br/> scanf ("% d", & X [I]); <br/>}</P> <p> // determine the initial conditions <br/> for (I = 1; I <= x [1]-1; I ++) <br/> F [I] = 0; <br/> X [M + 1] = N; <br/> F [x [1] = 1; </P> <p> for (k = 1; k <= m; k ++) <br/> {<br/> for (I = x [k] + 1; I <= x [k + 1]; I ++) <br/> {<br/> F [I] = 0; <br/> for (j = 1; j <= K; j ++) // perform grading based on formula accumulation <br/> F [I] = f [I] + F [I-X [J]; <br/> if (I = x [k + 1]) <br/> F [I] = f [I] + 1; <br/>}</P> <p> printf ("A total of different hop Methods: % d/N ", f [N]-1); </P> <p> return 0; <br/>}

 

 

Select from interesting C Programming

 

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.