Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to output a diamond with arbitrary side length in C language

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report