Algorithm note 11: printing patterns

Posted by nalleyp23 on Thu, 04 Nov 2021 17:25:20 +0100

1. Print diamond pattern    

  * 
 * * 
* * * 
 * * 
  * 
#include <stdio.h>

int main()
{
    int n = 0; 
    //Multi group input
    while (scanf("%d",&n)!=EOF)
    {
        //Print top half
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < n - i; j++)
            {
                printf(" ");
            }
            //Print * spaces
            for (int k = 0; k <= i; k++)
            {
                printf("* ");
            }
            printf("\n");
        }
        //Print bottom half
        //Number of control rows
        for (int i = 0; i < n + 1; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < i; j++)
            {
                printf(" ");
            }
            //Print * spaces
            for (int k = 0; k < n + 1 - i; k++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }

    return 0;
}

        Firstly, the whole diamond pattern is divided into upper and lower parts, with n lines in the upper part and n+1 lines in the lower part.                

         The key point of this question is that there are spaces first and then *. You can know that the entered number is the number of spaces in the first line. The number of spaces is decreasing and * is increasing. Then, because the space and * are not cross output, first traverse the space, then traverse the * sign, and consider them separately. The upper half of the control space print decrement is used   n - i, reduce the number of iterations per time.

2. Right triangle pattern with spaces

        * 
      * * 
    * * * 
  * * * * 
* * * * *
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            for (int j = 0; j < n; j++)
            {
                if (i + j < n - 1)
                {
                    printf("  ");
                }
                else
                {
                    printf("* ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

        Each time two spaces are printed, i+j is used to control the number of spaces currently printed. Because j controls the inner loop, when i gradually increases, (i+j < n - 1), j gradually decreases and the number of spaces printed becomes less.

3. Pyramid pattern

   * 
  * * 
 * * * 
* * * * 
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < n - 1 - i; j++)
            {
                printf(" ");
            }
            //Print * spaces
            for (int k = 0; k <= i; k++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }

    return 0;
}

            This question is equivalent to half of the diamond pattern. Print the space first, and then *,   (int j = 0; j < n - 1 - i; j++)   , The reason for n-1 is that there are only n-1 spaces at most, and - I is to make j print fewer spaces when the number of lines becomes larger* The maximum number of times is equal to the input number n, so < = n is OK.          

4. Flip pyramid pattern

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
# include <stdio.h>

int main()
{
    int n = 0; 
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < i; j++)
            {
                printf(" ");
            }
            //Print * spaces
            for (int k = 0; k < n - i; k++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }

    return 0;
}

        The reverse idea is the opposite. The space is increasing. The idea is still i to control the number of lines, and then traverse the space first and then *. At the beginning, because j and i are both 0, the space will not be output in the first line.

5. K-pattern

* * * 
* * 
* 
* * 
* * * 
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Print top half
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            //Print * spaces
            for (int j = 0; j < n + 1 - i; j++)
            {
                printf("* ");
            }  
            printf("\n");
        }
        //Print bottom half
        //Number of control rows
        for (int i = 0; i < n + 1; i++)
        {
            //Print one line
            //Print * spaces
            for (int j = 0; j <= i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }

    return 0;
}

          Like the diamond, it is divided into upper and lower parts. Because they all start from *, there is no need to consider spaces, but only consider decreasing and increasing separately. Suppose the input number is 2, then the first line n+1-i is 3, and print three *.

6. Arrow pattern

    *
  **
***
  **
    *
#include <stdio.h>

int main()
{
    int n = 0; 
    //Multi group input
    while (scanf("%d",&n)!=EOF)
    {
        //Print top half
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < n - i; j++)
            {
                printf("  ");
            }
            //Print * spaces
            for (int k = 0; k <= i; k++)
            {
                printf("*");
            }
            printf("\n");
        }
        //Print bottom half
        //Number of control rows
        for (int i = 0; i < n + 1; i++)
        {
            //Print one line
            //Print spaces
            for (int j = 0; j < i; j++)
            {
                printf("  ");
            }
            //Print * spaces
            for (int k = 0; k < n + 1 - i; k++)
            {
                printf("*");
            }
            printf("\n");
        }
    }

    return 0;
}

        Like the diamond code, it only outputs two spaces, * followed by no spaces.

7. Backslash pattern

*   
 *  
  * 
   *
#include<iostream>
#include<stdio.h>
#include<math.h>
#Include < string > / / String header file
#include <bits/stdc++.h>
#Include < algorithm > / / algorithm header file, including sort
int main()
{
    int n = 0;
    //Multi group input
    while (scanf("%d",&n)!=EOF)
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Control column
            for (int j = 0; j < n; j++)
            {
                //Control printing
                if (i == j)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

        Because the number of pattern columns is increasing, let j < n, the idea here is the same as the diagonal of the printing matrix, let i print * at the same position as j, and fill the rest with spaces.

8. Forward slash pattern

   *
  * 
 *  
*   
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Number of control columns
            for (int j = 0; j < n; j++)
            {
                //Judgment printing
                if (i + j == n - 1)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

        Because the last sign to be printed is the * sign, it is n-1 at the control position. With the increase of i, j also becomes smaller, and the position of the * sign is closer.

9. X pattern

*   *
 * * 
  *  
 * * 
*   *
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Number of control rows
        for (int i = 0; i < n; i++)
        {
            //Control column
            for (int j = 0; j < n; j++)
            {
                if (i == j || i + j == n - 1)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

        This pattern is equivalent to the addition of forward and backward slashes, so the judgment conditions are also equivalent to the addition.

10. Hollow square pattern

* * * * 
*     * 
*     * 
* * * * 
#include <stdio.h>

int main()
{
    int n = 0;
    while ((scanf("%d", &n)) != EOF)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (0 == i || n - 1 == i || 0 == j || n - 1 == j)
                {
                    printf("* ");
                }
                else 
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

        Output 0 rows and 0 columns, the last row and last column.

11. Hollow triangle pattern

*       
* *     
*   *   
* * * * 
#include <stdio.h>

int main()
{
    int n = 0;
    //Multi group input
    while (~scanf(" %d", &n))
    {
        //Control line
        for (int i = 0; i < n; i++)
        {
            //Control column
            for (int j = 0; j < n; j++)
            {
                //Control printing
                if (i == n - 1 || j == 0 || i == j)
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

        Output the first column and the last row, and the slash where i is equal to j

summary

         For the printing pattern, if it is symmetrical, it is divided into upper and lower layers, which are considered separately. The conditions for controlling the number of printing layers are constrained according to the increasing and decreasing conditions. Generally, i controls the number of rows and j controls the number of columns. If you traverse the space first and then * is two for loops of the same level, thinking alone to find the law

Topics: C Algorithm