当前位置:网站首页>[C language] print pattern summary
[C language] print pattern summary
2022-07-28 20:03:00 【An ran_】
Catalog
3、 ... and 、 Print specific graphics and some typical application problems
Preface :1. There are many ways of speaking here that may not be so rigorous , Just for the convenience of understanding and memory .
2. generally speaking , Print graphics , Some require the use of patterns , Some require figures , What is summarized here is with special symbols '*' For example , A summary of common print pattern problems .

【 It is divided into basic non format control and format control ( Need special loop control space )】
1. There is little need to control the format
1. Print rectangle
#include <stdio.h>
int main()
{
int length, width;
scanf("%d%d", &length, &width);
int i, j;
for (i = 1;i <= length;i++)
{
for (j = 1;j <= width;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
2. Print right triangle
#include <stdio.h>
int main()
{
int line;
scanf("%d", &line);
int i, j;
for (i = 0;i < line;i++)
{
for (j = 0;j <= i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
3. Print inverted triangle
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j;
for (j = 1;j <= n;j++)
{
for (i = j;i <= n;i++)
printf("* ");
printf("\n");
}
return 0;
} 
2. Circular control format is required
1. Print backslash
#include <stdio.h>
int main()
{
int i,j,k;
int n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
printf("*\n");
}
return 0;
}
2. Print forward slash
#include <stdio.h>
int main()
{
int i, j, k;
int n;
scanf("%d", &n);
for (i = 0;i < n;i++)
{
for (j = n - 1 - i;j > 0;j--)
{
printf(" ");
}
printf("*\n");
}
return 0;
}
3. Print pyramid ( an isosceles triangle )
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j, k;
for (i = 1;i <= n;i++)
{
for (k = 1;k <= (n - i);k++)
printf(" ");
for (j = 1;j <= i;j++)
printf("* ");
printf("\n");
}
return 0;
}
4. Print inverted triangle ( Inverted triangle )
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j, k;
for (i = 1;i <= n;i++)
{
for (k = 1;k < i;k++)
printf(" ");
for (j = 0;j <= n - i;j++)
printf("* ");
printf("\n");
}
return 0;
}
5. Print diamond
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j, k;
for (i = 1;i <= n;i++)
{
for (k = 0;k <= n - i;k++)
printf(" ");
for (j = 1;j <= i;j++)
printf("* ");
printf("\n");
}
for (i = 0;i < n + 1;i++)
printf("* ");
printf("\n");
for (i = 1;i <= n;i++)
{
for (k = 1;k <= i;k++)
printf(" ");
for (j = i;j <= n;j++)
printf("* ");
printf("\n");
}
return 0;
}
1. Print a hollow rectangle
#include <stdio.h>
int main()
{
int i, j, k;
int n,m;
scanf("%d%d", &n,&m);
for (i = 0;i < n;i++)
{
for (j = 0;j < m;j++)
{
if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
2. Print hollow triangles
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j;
for (i = 1;i <= n;i++)
{
for (j = 1;j <= i;j++)
{
if (i == j || i == 1 || i == n || j == 1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
3. Print white diamond
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i, j, k;
for (i = 1;i <= n;i++)
{
for (k = 0;k <= n - i;k++)
printf(" ");
for (j = 1;j <= i;j++)
if(j==1||j==i)
printf("* ");
else
printf(" ");
printf("\n");
}
for (i = 0;i < n + 1;i++)
if (i == 0 || i == n)
printf("* ");
else
printf(" ");
printf("\n");
for (i = 1;i <= n;i++)
{
for (k = 1;k <= i;k++)
printf(" ");
for (j = i;j <= n;j++)
if (j == i || j == n)
printf("* ");
else
printf(" ");
printf("\n");
}
return 0;
}
4.……
3、 ... and 、 Print specific graphics and some typical application problems
1. Print the Christmas tree
2. Print arrow
3. Print K
4. Print X
5. Yang hui triangle
……
The printing pattern is mainly for Cycle to achieve .
① One dimensional graph is generally a for Loop implementation , For example, the printing of line segments .
② Two dimensional graphics are generally for Nested by loops , For example, rectangle , right triangle , The pyramid , The diamond ……
Note here that if the first line of each line 0 The columns have patterns , For example, right triangle ( On the left ), Just control printing * The distance between , At the same time, the internal circulation condition here is generally for(j=1;j<=i;j++);
If it's like a pyramid , Except for the need to for Loop nesting controls the printing *, Also add an inner loop to specifically control spaces , This condition is generally for(k=1;k<=n-i;k++)
If it's like a diamond , Generally, it needs to print in blocks , It itself can be seen as the combination of two isosceles triangles with a line in the middle . Above is the pyramid , Below is the inverted pyramid ( Refer to the inverted pyramid printing method ).
③ Specific graphics / The printing of more intuitive graphics is generally a combination of several different types of graphics . So we can package these graphics into functions in the printing process , On this basis, discuss when to print these graphics . One of the most typical is the Christmas tree , It is a combination of several isosceles triangles and rectangles , Concrete realization , Refer to the printing of Christmas tree .
To make a long story short , I demonized it before , It led to cowardice at that time , Now go back to this module , Surprised, surprised , Find out “ It turns out that it's just like this ”~
Learn new knowledge , Through my own thinking 、 Practice and summary it's really fun to understand a kind of topic ~

边栏推荐
- MySQL8 基于clone创建主从复制
- Leetcode day3 employees who exceed the manager's income
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- leetcode day4 部门工资最高的员工
- NetCoreAPI操作Excel表格
- Nokia expands its 5g agreement with BT and will become its largest wireless access device supplier
- How does app automated testing achieve H5 testing
- Redis notes
- 2022年下半年系统集成项目管理工程师认证8月20日开班
- Source code analysis of scripy spider
猜你喜欢

Deploy LNMP automatically with saltstack

【NPP安装插件】

你知道雨的类型有几种?

A chip company fell in round B

Digital filter design matlab

Codeignier framework implements restful API interface programming

How does app automated testing achieve H5 testing

C language pointer and two-dimensional array

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!

【微信小程序开发】页面导航与传参
随机推荐
leetcode day4 部门工资最高的员工
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
MySQL命令语句(个人总结)
This customized keyboard turns me on~
MySQL性能测试工具sysbench学习
Array method added in ES6
CodeIgnier框架实现restful API接口编程
[experience] some suggestions and experience on repairing electronic equipment
Const pointer of C language and parameter passing of main function
认识中小型局域网WLAN
[NPP installation plug-in]
Redis笔记
Digital filter design matlab
Know small and medium LAN WLAN
Implementation of memmove in C language
Implementation of markdown editor in editor.md
String中常用的API
What is the process of swing event processing?
Overcome the "fear of looking at teeth", and we use technology to change the industry
Business visualization - let your flowchart "run" (4. Actual business scenario test)