Article Directory
foreword
Hello everyone, today I will share with you how to use the loop control process to output simple patterns. The explanation is very clear, come and have a look!
1. The principle of cycle control
Reasonable use of the for loop can complete many pattern operations. For example: rhombus, Yanghui triangle, pyramid, upper triangle, square, etc., we only need to figure out which line should control the output, how many should be output, where should add spaces, and how many spaces should be added, we can master it more skillfully The essence of loop control output simple patterns! ! !
2. Examples of simple patterns
1. Enriched square inside
This is the most basic pattern, let's use it to practice first, the pattern is as follows: We
can see from this pattern that it is composed of a certain number of asterisks and spaces in each line, and the number of rows and columns must be guaranteed Same, the code is shown as follows:
#include <stdio.h>
int main()
{
int n = 0;
while(scanf("%d", &n) != EOF)
{
int i = 0;
//确定行数
for(i=0; i<n; i++)
{
int j = 0;
//每一行的打印,打印n组*+空格
for(j=0; j<n; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
The author also added multiple sets of inputs, let us input a number, and then output a square of the size of the number. The
code runs as follows:
input 5, and output a 5*5 square
2. Right-angled triangle
The pattern is as follows:
in this pattern, we can see that the number of output per line is closely related to the number of lines, so we can use the number of lines as the output control. The code is as follows:
#include<stdio.h>
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
int i = 0;
//控制行数
for (i = 1; i <= n; i++)
{
//每一行
int j = 0;
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
We still use multiple sets of input, so that we can get multiple patterns. The
operation is as follows:
we input 6, and it outputs a right-angled triangle with the size of 6 rows for us.
3. Direct triangle in opposite direction
The pattern is as follows:
We can see from this pattern that each line has a different number of spaces output before outputting the asterisk, so this requires us to take the control output of spaces into account, the number of spaces in each line and the number of asterisks The change is just the opposite, and the asterisks are also output together with spaces. The change in the number of each line is still closely related to the number of lines. The code is as follows:
#include <stdio.h>
int main() {
int n=0;
while(~scanf("%d",&n))
{
int i=0;
//控制总行数
for(i=1;i<=n;i++)
{
//控制输出空格
for(int j=i;j<n;j++)
{
printf(" ");//这里是两个空格
}
//控制输出星号
for(int k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");//一行完了得换行
}
}
return 0;
}
The result of the operation is as follows:
4. Pyramid pattern on the front
The graph is as follows:
the pyramid graph has a certain number of spaces before the output of each asterisk, and the number of asterisks and spaces also changes with the number of lines, but they are closely related to the number of lines.
code show as below:
int main()
{
int n = 0;
while(scanf("%d", &n) != EOF)
{
int i = 0;
for(i=0; i<n; i++)
{
//一行
int j = 0;
//空格
for(j=0; j<n-1-i; j++)
{
printf(" ");
}
//*
for(j=0; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
The operation is as follows:
this pyramid can be easily completed as long as the number of spaces and asterisks output in each line is mastered. The number of spaces and asterisks output in each line is closely related to the change of the number of lines. We can easily get conclusion.
5. Hollow square
The graph is as follows:
This square is different from the first one, that is, there are a certain number of spaces inside it. Let’s look at the code first:
#include <stdio.h>
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
int i = 0;
int j = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == 0 || i == n - 1)
printf("* ");
else if (j == 0 || j == n - 1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
return 0;
}
You should be wondering why spaces and asterisks are output when certain conditions are met. Let me explain here:
According to the pattern, you can get: the first line and the last line have asterisk output, so the condition in the first if () is In the case of the first line and the last line, the else if() is the case of the first column and the last column, and spaces are output in the rest of the positions, which perfectly controls the output of the corresponding characters in the corresponding positions.
Run as follows:
6. Arrow Pattern
The pattern is as follows:
This is controlled by the upper part and the lower part. The author divides the middle line into the upper part (it can also be divided into the lower part). Every time you output a space, you need to output two to achieve the effect. , otherwise it cannot be completed, and then just figure out the logical relationship between the change in the number of rows, the number of columns and n, the code is as follows:
int main() {
int n = 0;
while (~scanf("%d", &n)) {
int i = 0, j = 0, k = 0;
//行数
for (i = 0; i <= n; i++) {
//空格
for (j = i; j < n; j++) {
printf(" ");
}
for (k = 0; k <= i; k++) {
printf("*");
}
printf("\n");
}
for (i = 0; i < n; i++) {
//空格
for (j = 0; j <=i; j++) {
printf(" ");
}
for (k = i; k <n; k++) {
printf("*");
}
printf("\n");
}
}
return 0;
}
Run as follows:
7. Rhombus
The rhombus should also be divided into upper and lower parts to control the output.
First look at the code display as follows:
int main()
{
int i = 0, j = 0, k = 0, l = 0;;
//行数
int n = 0;
scanf("%d", &n);
//控制输出上半部分
for (i = 0; i<=n; i++)
{
//控制输出空格
for (j = i; j < n; j++)
{
printf(" ");
}
//控制输出星号
for (k = 0; k <= i; k++)
{
printf("*");
}
for (l = 0; l < i; l++)
{
//这个循环是控制输出每行之间那个星号之后的星号,和中间之前的那部分数量是相同的
printf("*");
}
printf("\n");
}
//下面这里是控制下半部分
for (i = 0; i < n; i++)
{
//控制输出空格
for (j = 0; j <=i; j++)
{
printf(" ");
}
//控制输出星号
for (k = i; k <= n-1; k++)
{
printf("*");
}
for (l = i; l < n-1; l++)
{
printf("*");
}
printf("\n");
}
return 0;
}
The specific control situation is clearly marked in the code, you can check it!
Run as follows:
Summarize
The control of various simple patterns is nothing more than reasonably controlling the relationship between the number of rows and columns and the amount of change in the loop. This is the end of today's sharing. I believe we will have a lot of gains. If you like it, you can like it and follow it. Everyone who likes it will get an offer! ! !