The idea of output pattern class problem

Source: Internet
Author: User

problem Import --Write a program that uses only two output statements to generate a # symbol pattern like a half-5*5 square shape (right triangle):

#####

####

###

##

#

We can use subtraction, first think of it as a 5*5 rectangle.

First line: ##### implementation code (one for loop)

1  for (int15; hashnum++) 2     {3         printf ("#"); 4     }5     printf ("\ n");

So, to print out a 5*5 rectangle, just loop the above code 5 times, i.e.:

1  for(introw =1; row <=5; row++)2     {3          for(intHashnum =1; Hashnum <=5; hashnum++)4         {5printf"#");6         }7printf"\ n");8}

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

So how do you change the code so that it produces a half-square pattern?

If we look at the above program list and compare it to the output of the half square we need, we can see that the problem is that the conditional expression Hashnum <= 5. This condition produces 5 identical lines consisting of 5 # symbols. So we need a mechanism to adjust the number of symbols generated for each line, make the first behavior 5 symbols, the second one 4 symbols, and so on.

Next, we're going to experiment with another mitigation program: Write a piece of code, function: show numbers from 5 to 1, each in a separate line

Analysis: We must find an expression that has a value of 5 on the first row, a value of 4 on the second row, and so on. If we need an expression that decrements its value when the line number is incremented, the first thought might be a sign preceded by the row number, which is equivalent to multiplying it by-1. This method can produce a descending number, but not the number we need. We can summarize the analysis of the problem through a form.

Line number

The value that you want

Line number *-1

The difference between the line number and the desired 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 "show numbers from 5 to 1 and each number appears in a separate line" is:

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

So the full code of the # symbol pattern that prints half a 5*5 square shape (right triangle) is

1#include <stdio.h>2 intMain ()3 {4      for(introw =1; row <=5; row++)5     {6          for(intHashnum =1; Hashnum <=6-row;hashnum++)7         {8printf"#");9         }Tenprintf"\ n"); One     } A}

problem Extension -write a program that uses only two output statements to produce a # symbol pattern similar to the side triangle shape:

#

##

###

####

###

##

#

Based on the analysis of the previous question, we know that the following is the practice:

1, use a loop to display a line of symbols of a specific length.

2, use nested loops to display a series of rows.

3, use algebraic expressions instead of fixed values to create a different number of symbols for each line.

4, through the test and analysis, found the correct algebraic expression.

The various components needed to solve each shape problem

##### ##### 5

# # # ##### 4

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

# # ##### 2

# ##### 1

# # # # # 1

# # # # # 2

# # # # 3

# # # # # # # # 4

# # # # 3

# # # # # 2

# # # # # 1

The various components needed to solve the "side triangle" problem

1 7 3 3 #

2 6 2 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 "Half square" problem, it is feasible to subtract the line number with a large integer, so we use 8-row (line number) to get the result of (b), but this is not what we want. In the previous question, we needed numbers from large to small, not from small to large, so we could subtract the loop variable with a larger number. In this problem, we start from small to large and then from big to small. You can try to find an intermediate value, so we replace the previous 8-row with 4-row, and we can get the result of (c). The result is not correct, but if you ignore the minus sign to the left of the last 3 digits, it is the result we need. What happens if we use the absolute value function to get rid of these negative signs? The expression abs (4-row) produces the result (d), which is the result we want.

Extension Analysis: What happens if we count a space instead of a # number? The (d) column is the correct sequence of values for which we count the spaces. To get the correct number of # symbols, you can think of each line as having 4 squares and subtracting the number of spaces. If there are 4 squares per line, where ABS (4-row) is the number of spaces, then the number of squares with the # symbol can be obtained with 4-abs (4-row).

The code is as follows: (note To add the Stdlib.h header file before calling the ABS function)

1#include <stdio.h>2#include <stdlib.h>3 intMain ()4 {5      for(introw =1; row <=7; row++)6     {7          for(intHashnum =1; Hashnum <=4-abs (4-row); hashnum++)8         {9printf"#");Ten         } Oneprintf"\ n"); A     } -}

If again encounter similar pattern output class problem, above this set of combination fist is still very effective! ^_^

The idea of output pattern class problem

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.