Write algorithms step by step (to climb the stairs)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

When I got online two days ago, I saw a particularly interesting question. Here I will share with my friends:

 

One person is going to start to climb the stairs. Suppose there are n stairs. How many ways can this person climb one or two stairs at a time?

 

Before revealing the answer, you can consider the following:

 

The man climbed the n-tier stair, so it was not as high as it could all be, he had only two choices, either crawling from The N-2 layer or from the n-1 layer. He has no other options. At this point, I believe that my friends have already seen it. This is a basic recursive question.

 

(1) first, we create a function to determine the validity of the function.

 

 

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

Return;

}

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

Return;

}

(2) Determine whether the current number of layers is 1 or 2.

 

 

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

If (layer = 1 ){

Printf_layer_one (layer, stack, top );

Return;

}

If (layer = 2 ){

Printf_layer_two (layer, stack, top );

Return;

}

 

Return;

}

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

If (layer = 1 ){

Printf_layer_one (layer, stack, top );

Return;

}

If (layer = 2 ){

Printf_layer_two (layer, stack, top );

Return;

}

 

Return;

} (3) design the printing function mentioned in 2 and complete the code

 

 

# Define GENERAL_PRINT_MESSAGE (x )\

Do {\

Printf (# x );\

For (index = (* top)-1; index> = 0; index --)\

Printf ("% d", stack [index]); \

Printf ("\ n ");\

} While (0)

 

Void printf_layer_one (int layer, int * stack, int * top)

{

Int index;

GENERAL_PRINT_MESSAGE (1 );

}

 

Void printf_layer_two (int layer, int * stack, int * top)

{

Int index;

GENERAL_PRINT_MESSAGE (11 );

GENERAL_PRINT_MESSAGE (2 );

}

# Define GENERAL_PRINT_MESSAGE (x )\

Do {\

Printf (# x );\

For (index = (* top)-1; index> = 0; index --)\

Printf ("% d", stack [index]); \

Printf ("\ n ");\

} While (0)

 

Void printf_layer_one (int layer, int * stack, int * top)

{

Int index;

GENERAL_PRINT_MESSAGE (1 );

}

 

Void printf_layer_two (int layer, int * stack, int * top)

{

Int index;

GENERAL_PRINT_MESSAGE (11 );

GENERAL_PRINT_MESSAGE (2 );

} Note: a) the macro is used in the Code. Note that this is a do {} while (0) structure, and we perform String Conversion on x.

 

B) when the remaining step is 2, there are two situations: either one jump or two

 

 

 

 

(4) When the step is not 1 or 2, recursive processing is required.

 

 

Void _ jump_ladder (int layer, int * stack, int * top, int decrease)

{

Stack [(* top) ++] = decrease;

Jump_ladder (layer, stack, top );

Stack [-- (* top)] = 0;

}

 

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

If (layer = 1 ){

Printf_layer_one (layer, stack, top );

Return;

}

 

If (layer = 2 ){

Printf_layer_two (layer, stack, top );

Return;

}

 

_ Jump_ladder (layer-1, stack, top, 1 );

_ Jump_ladder (layer-2, stack, top, 2 );

}

Void _ jump_ladder (int layer, int * stack, int * top, int decrease)

{

Stack [(* top) ++] = decrease;

Jump_ladder (layer, stack, top );

Stack [-- (* top)] = 0;

}

 

Void jump_ladder (int layer, int * stack, int * top)

{

If (layer <= 0)

Return;

 

If (layer = 1 ){

Printf_layer_one (layer, stack, top );

Return;

}

 

If (layer = 2 ){

Printf_layer_two (layer, stack, top );

Return;

}

 

_ Jump_ladder (layer-1, stack, top, 1 );

_ Jump_ladder (layer-2, stack, top, 2 );

}

A function is added at the end of the function. It is mainly used to save some data to the stack during recursion. To make the code concise, We have redefined a function.

 

 

 

 

Summary:

 

1) this question is very similar to the Fibonacci series and is a recursive Question of the ground track.

 

2) recursive functions also need to be tested and used improperly, making stack overflow or endless loops very easy. In this regard, we can test the parameters in sequence from small to large. For example, we can test how to run when the stairs are 1, 2, and 3, and combine the manual computing with the program, constantly correct the code and improve the code.

 

 

Related Article

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.