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

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

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

Note: a) the macro is used in the Code. Note that this is a do {} while (0) structure. At the same time, 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);}

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.

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.