C + + Print triangles

Source: Internet
Author: User

Requires the use of the C + + language to print as a shape under a console project.

    

This is a different number of * symbols, different numbers of space characters, line breaks arranged in the 8-column horizontal lines, according to the regular arrangement of a positive triangle.

① first analyzes what we need to print:

    1. In addition to the last line (line eighth), there are a number of whitespace characters before the * symbol of the first line to the seventh line that makes up the triangle.
    2. The * symbol that makes up the triangle.
    3. A newline character between each row and the next line.

② Consider the number of prints per line (whitespace, * symbol, line break):

1. The first is the space character, according to the graphical observation can get the relationship between the number of spaces and line number:

Current line number The number of characters in the current line

1 7

2 6

3 5

4 4

5 3

6 2

....        ....

This concludes the formula: number of current spaces = Maximum line number-current line number

2. Next is the number of * symbols, as well as the number of symbols and line numbers can be obtained by observing the graphs:

Current line number Current line * character number

1 1

2 3

3 5

4 7

5 9

6 11

This concludes the formula: Current * Number = Current line number * 2-1

3, the last is a newline character, it is obvious that in addition to the last line of each of the remaining lines only need a newline character to print out the current line of space and * characters after the line.

③ finally we consider what idioms can be finished printing:

There are three types of loop structures in C + +, do and for, where the number of content to print is determined, so we prefer to use a for loop to accomplish this task.

The number of space characters, * symbols, and line breaks required to print above requires line numbers to participate in the calculation.

We declare an int type variable i to represent the current line number, using I to replace the above formula:

I line number of spaces = Maximum line number-I

I line * Number of characters = i * 2-1

I line break number: 1

Get the following code:

int i,j,k;

for (i = 1; I <= 8; i++) {//The control row determines the current line number at the time of the loop, and in turn corresponds to the 1~8 row of the triangle.
for (j = 1; J <= 8-i; j + +) {//The control line determines the number of characters to print, and I determines the line number.
printf ("");
}
for (k = 1; K <= i*2-1; k++) {//The control line determines the number of print * symbols, and I determines the line number.
printf ("*");
}
printf ("\ n"); Print a newline character per line.
}

The final code is not the key, and you should focus on the analysis process when you see the problem.

C + + Print triangles

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.