当前位置:网站首页>[C language practice - printing square and its deformation]
[C language practice - printing square and its deformation]
2022-06-09 13:07:00 【Beginners of C language】
Print squares and their deformations
Preface
A square is a special parallelogram , See Baidu Encyclopedia for the definition

This article is mainly about writing diamond exercises , Print diamonds on the screen .

1、 Print graphics 1
// Print squares and their deformations
int main()
{
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)// Row number
{
for (int j = 0; j < n; j++)// Number of columns
{
printf("*");
}
printf("\n");
}
return 0;
}
The results are as follows , Although it is 10 That's ok 10 Column , Because there is no space between characters in the same line , And between different lines due to line breaks , The printed graphic does not look square , It's a rectangle :

2、 Print graphics 2
// Print squares and their deformations
int main()
{
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)// Row number
{
for (int j = 0; j < n; j++)// Number of columns
{
printf("* ");
}
printf("\n");
}
return 0;
}
The results are as follows , It looks more like a square , But in fact, it is related to graphics 1 It is one thing .

3、 Print graphics 3
Print a square , rotate 45 degree
// Print a square , rotate 45 degree , No blank space version
int main()
{
int n = 0;
scanf("%d", &n);
// Print the top half n
for (int i = 0; i < n; i++)
{
// Print a row
// Print space
int j = 0;
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
// Print *
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
printf("\n");
}
// Print the bottom half n-1
for (int i = 0; i < n - 1; i++)
{
// Print a row
// Print space
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
// Print *
for (j = 0; j < (n - 1 - i) * 2 - 1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
** The results are as follows ,** It is still due to the inconsistent spacing of rows and columns , The square becomes a diamond :

4、 Print graphics 4
// Print a square , rotate 45 degree , Blank version
int main()
{
int n = 0;
scanf("%d", &n);
// Print the top half n
for (int i = 0; i < n; i++)
{
// Print a row
// Print space
int j = 0;
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
// Print *
for (j = 0; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
// Print the bottom half n-1
for (int i = 0; i < n - 1; i++)
{
// Print a row
// Print space
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
// Print *
for (j = 0; j <(n-1-i); j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
The results are as follows , The square is also a diamond , And graphics 3 The difference is whether there are spaces :

summary
This article exercises printing squares and their deformations , Mainly practice :
- Outer loop 、 Application of internal circulation
- Pay attention to the number of lines 、 Space number 、 Symbol * The mathematical relationship between numbers
I didn't expect it before printing , The row and column intervals are different , The length and width of the square will be different . It can be seen that only the print results can verify whether the implementation results of your own code are consistent with your own ideas . The square became a combination of rectangle and diamond , I feel like I have written another article .
边栏推荐
- 市面常用芯片对应的ARM架构
- 【 日志系统 】
- Method area of JVM runtime memory area family
- ep240--leding
- 数据库day-6
- Detailed explanation of three value transfer methods between wechat applet pages
- Loki: like Prometheus, but for logs
- JSON and JQ implementation of three-level linkage selection box method 1
- [interpretation of redis underlying mechanism: Linux operating system file descriptor FD]
- 【leetcode周赛记录】第294场周赛记录
猜你喜欢

射频同轴连接器和电缆指南--【转自微信公众号射频课堂】

High CPU utilization and detailed explanation of top command

【STM32】HAL库在4针脚0.96寸OLED屏上的移植---硬件IIC(一)

市面常用芯片对应的ARM架构
![[STM32] Hal library DAC](/img/fe/4d7c1a692c93cc67f7f0e11c9152aa.png)
[STM32] Hal library DAC

数据库day-1

Introduction to culture CNI topic 3: simple source code analysis of multi network CNI plug-ins

JVM运行时内存区系列之方法区

How to do the third question of multi table query in MySQL database?

Execution engine of JVM family
随机推荐
CK、ES、RediSearch 对比,谁的性能更胜一筹
『忘了再学』Shell基础 — 28、AWK中条件表达式说明
C#/VB. Net to add barcodes to PDF tables
C语言 队列--顺序队列
数据库day-3
Who has better performance than CK, ES and redisearch
机器学习-学习笔记(二) --&gt; 模型评估与选择
Handlebars.js模版引擎用法一
如何做数据可视化分析
【STM32】HAL库在4针脚0.96寸OLED屏上的移植---硬件IIC(一)
shell 参数里使用变量的方法
Core implementation of Kube apiserver scheduler
[detailed explanation of client list]
【leetcode周赛记录】第79场双周赛+第295场周赛记录
ES6 basic grammar knowledge
数据库day-5
[idea imports gradle project and starts]
常见的数据分析误区
Find container by overlay2 file name
【IDEA导入Gradle项目,并启动】