Idea of the output pattern problem and idea of the output pattern

Source: Internet
Author: User

Idea of the output pattern problem and idea of the output pattern

Problem Import-- Compile a program and use only two output statements to generate a # symbol pattern like a half-5-5-square shape (Cartesian triangle:

#####

####

###

##

#

 

We can use the Subtraction Method to first think of it as a 5*5 rectangle.

Implementation Code of the first line: ##### (A for loop)

1 for(int hashNum = 1;hashNum <= 5;hashNum++)2     {3         printf("#");4     }5     printf("\n");

Therefore, to print a 5*5 rectangle, you only need to loop the code five times, that is:

1 for(int row = 1;row <= 5;row++)2     {3         for(int hashNum = 1;hashNum <= 5;hashNum++)4         {5         printf("#");6         }7         printf("\n");8     }

So far, we can print a 5*5 rectangle, which is close to the final solution.

Then, how should we modify the code to generate a half-square pattern?

If we observe the program list above and compare it with the semi-square output we need, we can find that the problem lies in the condition expression hashNum <= 5. This condition generates five identical rows consisting of five # symbols. Therefore, we need a mechanism to adjust the number of symbols generated by each line, so that the first behavior has five symbols, the second behavior has four symbols, and so on.

Next, we will try another program to reduce the number: write a piece of code, function: display the number from 5 to 1, each number appears in a separate row

Analysis: we must find an expression with a value of 5 in the first row, 4 in the second row, and so on. If we need an expression for decreasing the value of a row when it increases progressively, we may first consider adding a symbol before the row number, which is equivalent to multiplying it by-1. this method produces a decimal number, but not the number we need. We can summarize the problem analysis in a table.

Row number

Required value

Row number *-1

Difference between the row number and the required value

1

5

-5

6

2

4

-4

6

3

3

-3

6

4

2

-2

6

5

1

-1

6

The difference is a fixed value of 6. This means that the expression we need is row *-1 + 6. Simplified to 6-row.

So the code for "displaying numbers from 5 to 1, each number appears in a separate row" is:

1     for(int row = 1;row <= 5;row++)2     {3         printf("%d\n",(6-row));4     }

So the complete code for printing half a 5*5 square shape (right triangle) # symbol pattern is

 1 #include<stdio.h> 2 int main() 3 { 4     for(int row = 1;row <= 5;row++) 5     { 6         for(int hashNum = 1;hashNum <= 6-row;hashNum++) 7         { 8             printf("#"); 9         }10         printf("\n");11     }12 } 

 

Problem Extension-- Compile a program and use only two output statements to generate a side-triangle shape # symbol pattern:

#

##

###

####

###

##

#

Based on the analysis of the previous problem, we know the practice is as follows:

1. Use a loop to display symbols of a specific length.

2. Use nested loops to display a series of rows.

3. Use an algebraic expression instead of a fixed value to create different numbers of symbols for each row.

4. Test and Analysis to find the correct algebraic expression.

Components required to solve various shape Problems

######### 5

######## 4

############ 3

####### 2

###### 1

 

##### 1

###### 2

####### 3

########### 4

####### 3

###### 2

##### 1

 

Components required to solve the "side triangle" Problem

1 7 3 3 #

2 6 2 ##

3 5 1 1 ###

4 4 0 0 ####

3 3-1 1 ###

2 2-2 2 ##

1 1-3 3 #

(A) (B) (c) (d) (e)

Analysis: In the "semi-square" problem, it is feasible to subtract a large integer from the row number. Therefore, this time we use 8-row (row number) to get the result of (B, but this is not what we want. In the previous question, we needed a large to small number instead of a small to large number, so we could just use a large number to subtract the cyclic variable. In this case, we first grow from small to large and then from large to small. We can try to find an intermediate value, so we can replace the previous 8-row with 4-row to get the result of (c. This result is incorrect, but if the negative number on the left of the last three numbers is ignored, it is the result we need. What if we use the absolute value function to remove these negative numbers? The expression abs (4-row) produces the result (d), which is the expected result.

Extended analysis: What if we count spaces instead of # numbers? (D) column is the correct value sequence for counting spaces. To get the correct number of # symbols, You can regard each line as having four grids and then subtract the number of spaces. If each row has four grids, Where abs (4-row) is the number of spaces, the number of grids with the # symbol can be obtained using 4-abs (4-row.

The Code is as follows: (Note that the stdlib. h header file must be added before calling the abs function)

 1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5     for(int row = 1;row <= 7;row++) 6     { 7         for(int hashNum = 1;hashNum <= 4-abs(4 - row);hashNum++) 8         { 9             printf("#");10         }11         printf("\n");12     }13 } 

If you encounter a similar pattern output problem again, the above combination is very effective! Pai_^

 

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.