How to output a diamond with arbitrary side length in C language
Editor to share with you how the C language output arbitrary side length of the diamond, I hope you will learn something after reading this article, let's discuss it together!
1. Simply understand the principle of asterisk output, divide the output asterisk code into two parts: the upper part and the lower part
First of all, we can observe that if the side length is a, then the length of the longest line is 2*a-1, and at the same time, each column of asterisks is aligned, which shows that if we want to output asterisks, there is a rule to follow, where there is no asterisk, we can replace it with spaces. It seems that we can use x-axis intervals to control the output of spaces and asterisks.
For example, in the third line, if we set the x coordinate of the first asterisk to 1 and output five asterisks on this basis, then we only need to write a string of code to control the output of the asterisk on the interval where x belongs to [1 ~ 5].
Similarly, for the first line in the picture, we only need to write a string of code to control the output of an asterisk on the interval where x belongs to (2p4), as follows
For (int job1th j2roomj)