From programming method to thinking training summary

Source: Internet
Author: User

This article through a few examples to describe the programming method , through the program design method to achieve the goal of thinking training .

The main core design methods are Pseudo-code method and decomposition method :

    1. Pseudo-code method: The idea used to describe a program , which can also be used to annotate
    2. Decomposition method: Gradual refinement , decomposition steps

Here are a few examples to learn the pseudo-code method and decomposition method . (SD: The following code is written in Python language)

Sample index:

    • Pharaoh's Pyramid
    • Throw a handkerchief

Example one, the Pharaoh's Pyramid

Topic:

Write a program, enter the number of layers, print out a pyramid of any number of layers in the console,

  

Analysis:

Depending on the topic, the pyramid graph consists of spaces and asterisks, now set the pyramid to 3 layers,height = 3

To solve this problem, first make a table:

Level N m

No. 0 Floor: Space 2, star 1
1th Floor: Space 1, star 3
2nd Floor: Space 0, star 5

When the number of layers is No. 0, the space is two , the asterisk is one ;

When the layer is the 1th layer is a space for one , the asterisk is three ;

When the number of layers is 2nd, the space is 0 , the asterisk is five ;

Can be listed as:n = height-1-level;

       m = 2*level + 1

According to the pseudo-code method, you can write the program ideas First:

    • Enter the number of layers first, and then print the graph, the input layer is very simple, as long as "height = Int (raw_input (' Ennter a integer ':))" can appear on the console "Ennter a Integer": ", and then enter the number;
    • Printing the pyramid is slightly more complex, which is the decomposition of the steps can be decomposed, the complex problems gradually refined.
      • You can print this part of the pyramid as: 1, Print n spaces, 2, print M stars.
      • Then use the For loop to print out the pyramid, specific code and effects

  

Enter the number of layers on the console to print the desired pattern at will.

  The idea of decomposition can be generalized in any programming, followed by the decomposition of programs in two ways.

1, the decomposition of the program structure (from the grammatical point of view):

Decomposition method can be used to decompose the program

The largest component of a program is a file, a file is decomposed into several functions, a function is decomposed into a statement block, a statement block is decomposed into a statement, a statement is decomposed into an expression, a variable, an operator

It is also possible to reverse aggregation: expressions, variables, operators are aggregated into statements, statements are aggregated into idioms, statements blocks are aggregated into functions, and several functions are aggregated into one file


2, the decomposition of the program structure (from the functional point of view):

Decomposition into: input, calculation (or processing), output

Input: Prompt information (can not), input method "Cui or GUI user interactive input, function entry parameters, command line arguments, file read, other devices", data legitimacy check or assertion

Calculation (or processing): a narrow calculation of "arithmetic operations (subtraction), logical operations (and or non), relational operations (greater than or equal to less than)"

Generalized computing: Program flow and algorithms

Processing: Pseudo-code method or decomposition method can be used

Output: Screen output, printer output, file output, function return value, function exit parameters

  Then the thinking training: Simulation of Reality (to copy the real problem to the computer)

According to the reality of the problem, draw a graph to instantiate it, with the inverse instantiation of the figure in the form of a digital expression, then find a data structure to express the problem, and finally design an algorithm to write code, summed up there are five steps:

Real World Computer World

Problem-Graphics (instantiation)--Mathematics (inverse instantiation)- - data structure--algorithm

Step0--step1--step2--step3--step4

Next, we use an example (drop handkerchief) to learn the simulation method, which is also used as code method and decomposition method.

  Second, drop the handkerchief

STEP0: Natural Language (Topic)

  

Step1: Graphics (set 5 people, drawing), the problem is instantiated, set 5 people to participate in the game, the number of each person, starting from the first person to shout 1, to shout 3 of the people out, the next continue to report 1.

To the last one to return the first count, and finally get the rest of the man four

  

Step2: In Mathematical language description (define the number of N), set the first for the POS0, out of the POS1, that count to become according to Pos0 find POS1, according to POS1 again find POS0.

(POS0--POS1--POS0--POS1), and note two conditions: 1, Team tail; 2. The next one is out.

Step3: Find the data structure (array), create an array p[100], p[0] to count out the number of people, p[1] = 1,p[2]=2,p[3]= 3,p[4] = 4,p[5] = 5;

So when P[0]=4, the last one left. The outgoing p[pos1]= 10 to indicate that it is out.

STEP4: Find the algorithm (Nextpos), find the next position, Nextpos represents the next position.

1 2 3
Pos0--pos1

Pos0 to POS1 to go through two positions, so Nextpos again Nextpos

  POS1 to Pos0 to Nextpos.

Consider both the end of the team and the next exit situation.

Write the above program code,

    

Algorithm:

  

Use decomposition Method:

  

From programming method to thinking training summary

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.