📜  程序打印双向箭头图案

📅  最后修改于: 2021-05-08 17:31:39             🧑  作者: Mango

给定整数N(其为数),任务是绘制双箭头形状的数字图案。

先决条件:该模式是增长和收缩类型的模式,因此需要执行循环的基础知识才能理解主题和任何语言的代码。几何形状可以可视化为-

例子:

Input: R = 7
Output:   
                  1
               2 1 1 2
            3 2 1   1 2 3
         4 3 2 1     1 2 3 4
            3 2 1   1 2 3
               2 1 1 2
                  1

Input: R = 9
Output:
                       1
                    2 1 1 2
                 3 2 1   1 2 3
              4 3 2 1     1 2 3 4
           5 4 3 2 1       1 2 3 4 5
              4 3 2 1     1 2 3 4
                 3 2 1   1 2 3
                    2 1 1 2
                       1

方法:

  1. 在给定的示例中,N = 7,ROWS的数目为7。
  2. 垂直地,图案增长到ROW = N / 2,然后收缩。
  3. 第1行有4个“(空格)字符,然后是一个值。
  4. 在每个连续的行中,SPACE字符的数量减少,而NUMERALS个计数增加。
  5. 另外,请注意,每行中放置的数字的第1个值与该行的数目相同。
  6. 同样,水平模式具有NUMERALS,然后是SPACES和NUMERALS。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the requried pattern
void drawPattern(int N)
{
    int n = N;
    int row = 1;
  
    // 'nst' is the number of values
    int nst = 1;
  
    // 'nsp' is the number of spaces
    int nsp1 = n - 1;
    int nsp2 = -1;
    int val1 = row;
    int val2 = 1;
  
    while (row <= n) {
  
        // Here spaces are printed
        // 'csp' is the count of spaces
        int csp1 = 1;
        while (csp1 <= nsp1) {
            cout << " "
                 << " ";
            csp1 = csp1 + 1;
        }
  
        // Now, values are printed
        // 'cst' is the count of stars
        int cst1 = 1;
        while (cst1 <= nst) {
            cout << val1 << " ";
            val1 = val1 - 1;
            cst1 = cst1 + 1;
        }
  
        // Again spaces have to be printed
        int csp2 = 1;
        while (csp2 <= nsp2) {
            cout << " "
                 << " ";
            csp2 = csp2 + 1;
        }
  
        // Again values have to be printed
        if (row != 1 && row != n) {
            int cst2 = 1;
            while (cst2 <= nst) {
                cout << val2 << " ";
                val2 = val2 + 1;
                cst2 = cst2 + 1;
            }
        }
        cout << endl;
  
        // Move to the next row
        if (row <= n / 2) {
            nst = nst + 1;
            nsp1 = nsp1 - 2;
            nsp2 = nsp2 + 2;
            val1 = row + 1;
            val2 = 1;
        }
        else {
            nst = nst - 1;
            nsp1 = nsp1 + 2;
            nsp2 = nsp2 - 2;
            val1 = n - row;
            val2 = 1;
        }
        row = row + 1;
    }
}
  
// Driver code
int main()
{
    // Number of rows
    int N = 7;
  
    drawPattern(N);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to print the requried pattern
    static void drawPattern(int N)
    {
        int n = N;
        int row = 1;
  
        // 'nst' is the number of values
        int nst = 1;
  
        // 'nsp' is the number of spaces
        int nsp1 = n - 1;
        int nsp2 = -1;
        int val1 = row;
        int val2 = 1;
  
        while (row <= n) {
  
            // Here spaces are printed
            // 'csp' is the count of spaces
            int csp1 = 1;
            while (csp1 <= nsp1) {
                System.out.print("  ");
                csp1 = csp1 + 1;
            }
  
            // Now, values are printed
            // 'cst' is the count of stars
            int cst1 = 1;
            while (cst1 <= nst) {
                System.out.print(val1 + " ");
                val1 = val1 - 1;
                cst1 = cst1 + 1;
            }
  
            // Again spaces have to be printed
            int csp2 = 1;
            while (csp2 <= nsp2) {
                System.out.print("  ");
                csp2 = csp2 + 1;
            }
  
            // Again values have to be printed
            if (row != 1 && row != n) {
                int cst2 = 1;
                while (cst2 <= nst) {
                    System.out.print(val2 + " ");
                    val2 = val2 + 1;
                    cst2 = cst2 + 1;
                }
            }
            System.out.println();
  
            // Move to the next row
            if (row <= n / 2) {
                nst = nst + 1;
                nsp1 = nsp1 - 2;
                nsp2 = nsp2 + 2;
                val1 = row + 1;
                val2 = 1;
            }
            else {
                nst = nst - 1;
                nsp1 = nsp1 + 2;
                nsp2 = nsp2 - 2;
                val1 = n - row;
                val2 = 1;
            }
            row = row + 1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Number of rows
        int N = 7;
  
        drawPattern(N);
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to print the requried pattern 
def drawPattern(N) : 
  
    n = N; 
    row = 1; 
  
    # 'nst' is the number of values 
    nst = 1; 
  
    # 'nsp' is the number of spaces 
    nsp1 = n - 1; 
    nsp2 = -1; 
    val1 = row; 
    val2 = 1; 
  
    while (row <= n) :
  
        # Here spaces are printed 
        # 'csp' is the count of spaces 
        csp1 = 1; 
        while (csp1 <= nsp1) :
            print(" ",end= " ");
            csp1 = csp1 + 1; 
  
        # Now, values are printed 
        # 'cst' is the count of stars 
        cst1 = 1; 
        while (cst1 <= nst) :
            print(val1,end = " "); 
            val1 = val1 - 1; 
            cst1 = cst1 + 1; 
  
        # Again spaces have to be printed 
        csp2 = 1; 
        while (csp2 <= nsp2) :
            print(" ",end = " "); 
            csp2 = csp2 + 1; 
  
        # Again values have to be printed 
        if (row != 1 and row != n) :
            cst2 = 1; 
              
            while (cst2 <= nst) :
                print(val2,end = " "); 
                val2 = val2 + 1; 
                cst2 = cst2 + 1; 
          
        print()
  
        # Move to the next row 
        if (row <= n // 2) :
            nst = nst + 1; 
            nsp1 = nsp1 - 2; 
            nsp2 = nsp2 + 2; 
            val1 = row + 1; 
            val2 = 1; 
  
        else :
            nst = nst - 1; 
            nsp1 = nsp1 + 2; 
            nsp2 = nsp2 - 2; 
            val1 = n - row; 
            val2 = 1; 
          
        row = row + 1; 
  
# Driver code 
if __name__ == "__main__" : 
  
    # Number of rows 
    N = 7; 
  
    drawPattern(N); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to print the requried pattern
    static void drawPattern(int N)
    {
        int n = N;
        int row = 1;
  
        // 'nst' is the number of values
        int nst = 1;
  
        // 'nsp' is the number of spaces
        int nsp1 = n - 1;
        int nsp2 = -1;
        int val1 = row;
        int val2 = 1;
  
        while (row <= n) 
        {
  
            // Here spaces are printed
            // 'csp' is the count of spaces
            int csp1 = 1;
            while (csp1 <= nsp1)
            {
                Console.Write(" ");
                csp1 = csp1 + 1;
            }
  
            // Now, values are printed
            // 'cst' is the count of stars
            int cst1 = 1;
            while (cst1 <= nst) 
            {
                Console.Write(val1 + " ");
                val1 = val1 - 1;
                cst1 = cst1 + 1;
            }
  
            // Again spaces have to be printed
            int csp2 = 1;
            while (csp2 <= nsp2) 
            {
                Console.Write(" ");
                csp2 = csp2 + 1;
            }
  
            // Again values have to be printed
            if (row != 1 && row != n)
            {
                int cst2 = 1;
                while (cst2 <= nst) 
                {
                    Console.Write(val2 + " ");
                    val2 = val2 + 1;
                    cst2 = cst2 + 1;
                }
            }
            Console.WriteLine();
  
            // Move to the next row
            if (row <= n / 2)
            {
                nst = nst + 1;
                nsp1 = nsp1 - 2;
                nsp2 = nsp2 + 2;
                val1 = row + 1;
                val2 = 1;
            }
            else 
            {
                nst = nst - 1;
                nsp1 = nsp1 + 2;
                nsp2 = nsp2 - 2;
                val1 = n - row;
                val2 = 1;
            }
            row = row + 1;
        }
    }
  
    // Driver code
    public static void Main()
    {
        // Number of rows
        int N = 7;
  
        drawPattern(N);
    }
}
  
// This code is contributed by AnkitRai01


输出:
1 
        2 1   1 2 
    3 2 1       1 2 3 
4 3 2 1           1 2 3 4 
    3 2 1       1 2 3 
        2 1   1 2 
            1

时间复杂度: O(N 2 )
空间复杂度: O(1)