Step by step or step by step-answers to exercise 14 in mad man C (chapter 3 Exercise 4)

Source: Internet
Author: User

Question:

4. How many different steps can there be when there are 6 levels of stairs and each step can only Span one or two levels?

This question may be a little difficult from the mathematical point of view, but it will be no longer difficult once it is broken.
First, there is only one way to go to the level 1 step. Two steps are available for the Level 1 step, because the steps can be directly across the top, or from the level 1 to the top.
In other words, because the number of steps that are attached to a Level 2 can be increased from level 1 (Initial State) or from level 2nd
The number of steps on the 2nd level step = the number of steps on the 0th level step + the number of steps on the 1st level step
Among them, the number of steps on the 0th level is obviously one.
This is just like there are three land and two waterways in a place, so there are five roads in the place.
The method for finding the numbers of the following steps is similar to the method for finding the order 2nd.
The code that describes the solution process is:

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Int num_of_step0 = 1, num_of_step1 = 1, num_of_step2 ,,
Num_of_step3, num_of_step4, num_of_step5, num_of_step6;

Num_of_step2 = num_of_step1 + num_of_step0;
Num_of_step3 = num_of_step2 + num_of_step1;
Num_of_step4 = num_of_step3 + num_of_step2;
Num_of_step5 = num_of_step4 + num_of_step3;
Num_of_step6 = num_of_step5 + num_of_step4;

Printf ("hundreds of thousands of steps have % d different steps \ n", num_of_step6 );

System ("pause ");
Return 0;
}

The disadvantage of this writing is that there are too many variables. If there are more orders, more variables need to be defined. In this way, even if you don't get tired, you will be stupid.
This method seems to have gone from level 2nd to level 6th. If there are many steps, it is really tiring.
In fact, taking steps is a kind of idea from the subjective perspective of people. It is natural to care about the number of steps in this situation. But if you change the situation and use your imagination a little bit, it is not difficult to find that if a person does not move the steps in the opposite direction, the nature of the problem is the same. At this time, you will not care about the total number of steps. You only need to care about the next two steps and the current steps. That is
Number of steps on the current step = number of steps on the back of 1st + number of steps on the back of 2nd
It is not hard to see that this is a method of thinking and Description Based on "relative", and the previous writing rule is based on the "absolute" method of thinking and description.
After a step is taken, the status changes. The next 1st steps have become the next 2nd steps, which can be described
The number of steps at the bottom of step 1 = the number of steps at the bottom of step 2
However, the current steps have become the next 1st steps.
Number of steps on the back of step 1 = number of steps on the current step
Now you will find that what you want to solve is exactly the same as what you just solved. The following code can be processed.
It is pleasant to solve the same problem repeatedly, and such code is not easy to write. You just need to carefully write the first pair and copy and paste it. Code:

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Int num_of_current_step, // number of steps currently facing
Num_of_passed_step1, // number of steps following the 1st-level step
Num_of_passed_step2; // number of steps following the 2nd-level hierarchy

// At level 0th
Num_of_current_step = 1; // Based on Analysis

// At level 1st
Num_of_passed_step1 = num_of_current_step; // the level 1 of the current backend
Num_of_current_step = 1; // Based on Analysis

// At level 2nd
Num_of_passed_step2 = num_of_passed_step1; // Step 1
Num_of_passed_step1 = num_of_current_step; // step back
Num_of_current_step = num_of_passed_step1
+ Num_of_passed_step2; // The current value is equal to the sum of the next two orders.

// At level 3rd. You can copy and paste it from here.
Num_of_passed_step2 = num_of_passed_step1; // Step 1
Num_of_passed_step1 = num_of_current_step; // step back
Num_of_current_step = num_of_passed_step1
+ Num_of_passed_step2; // The current value is equal to the sum of the next two orders.

// At level 4th
Num_of_passed_step2 = num_of_passed_step1; // Step 1
Num_of_passed_step1 = num_of_current_step; // step back
Num_of_current_step = num_of_passed_step1
+ Num_of_passed_step2; // The current value is equal to the sum of the next two orders.

// At level 5th
Num_of_passed_step2 = num_of_passed_step1; // Step 1
Num_of_passed_step1 = num_of_current_step; // step back
Num_of_current_step = num_of_passed_step1
+ Num_of_passed_step2; // The current value is equal to the sum of the next two orders.
// At level 6th
Num_of_passed_step2 = num_of_passed_step1; // Step 1
Num_of_passed_step1 = num_of_current_step; // step back
Num_of_current_step = num_of_passed_step1
+ Num_of_passed_step2; // The current value is equal to the sum of the next two orders.

Printf ("hundreds of thousands of steps have % d different steps \ n", num_of_current_step );

System ("pause ");
Return 0;
}

This method is undoubtedly simpler and more powerful, because there are few variables and it is easy to expand to solve the same problem of other orders. More importantly, the subsequent code segments can be simply copied and pasted without any modifications.
Some people may think that the code in the 2nd statements is too long. This problem can be easily solved after learning loop control statements. The simple structure of the code is more important. The 2nd code segment is simple because it is doing the same thing repeatedly (this is precisely the computer's strength. In the first piece of code, since the variable name needs to be modified after copying and pasting, in a sense, the structure is constantly changing and thus complicated. Besides, modifying the variable name is prone to errors, which can be considered more complex.
As long as possible, writing simple code is a basic principle of programming. This is the so-called KISS Principle-Keep It Simple Stupid.
However, we can see from the beginning that writing simple and error-prone code often requires a lot of brains, while complicated and error-prone code is often the product of one go without thinking.
There is a famous saying: it is very complicated to make things simple, and it is very easy to make things complex.
We can also understand that writing code is simple, but thinking before writing code is complicated.
For experts, once you start writing code, it often means that the code is about to be completed. What seems simple and easy to complete, in fact, I did not know how long it took to work hard in my mind.
Beginners often start to write code as soon as they get their questions. This is a bad programming habit and will never write good 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.