📜  如何轻松学习图案打印?

📅  最后修改于: 2021-05-08 18:54:54             🧑  作者: Mango

任务是为给定的整数值打印示例中所示的模式。

目的不是仅仅打印这种模式,而是要学习解决此类问题的最佳方法,因为在编码考试和工作面试中经常会问这些问题。

例子:

Input: N = 4
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Input: N = 7
Output:
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7

对于本教程,使用了N = 4的示例。

  • 步骤1:首先,分析图案的任何对称线。在这里,我们的图案在垂直和水平方向上都是对称的,因此绘制这样的对称线,

    在将图案分成几部分后,首先尝试仅绘制左上部分,即A部分。如果没有任何对称线,请跳至步骤2。

  • 第2步:现在将每个单元格(即元素)与行号和列号相关联(通常分别由i和j表示),就像

    从现在开始,一个单元格用C(i,j)表示,其行号和列号为。

  • 步骤3:在此步骤中,尝试查找C(i,j)的值与i和/或j之间的关系。通常,C的值取决于N的值以及i和j的相对值。详细说明,

    –在第一行中,每个元素都相同,即4(= N)。因此,它没有太大帮助。
    –在第二行中,对于i> = j,可以看到C从4减少到3,然后对于下一个i = j,它保持3,然后对于所有j的值,对于下一个i = j,它保持2,可以看到C从4减少到1。

    因此,在每一行中,C从N开始,递减1直到i> = j,然后变为常数。这里的C值取决于i和j之间的较小者,公式可以是:

    C(i, j) = N - min(i, j) + 1
    

    因此,我们的方法应该是:

    C++
    #include 
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }


    Java
    // Java program to find triplets 
    // a[i]>a[j]>a[k] and i


    Python3
    # Python3 program to find triplets 
    # a[i]>a[j]>a[k] and i


    C#
    // C# program to find triplets 
    // a[i]>a[j]>a[k] and i


    C++
    #include 
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }


    Java
    class GFG 
    {
        public static void main(String[] args) 
        {
            int N = 4, i, j, min;
            System.out.println("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                System.out.println();
            }
        }
    }
      
    // This code is contributed by PrinciRaj1992


    Python3
    N = 4;
    print("Value of N: ", N);
      
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            min = i if i < j else j;
            print(N - min + 1, end = "");
      
        for j in range(N - 1, 0, -1):
            min = i if i < j else j;
            print(N - min + 1, end = "");
        print();
      
    # This code is contributed by Rajput-Ji


    C#
    using System;
      
    class GFG 
    {
        public static void Main(String[] args) 
        {
            int N = 4, i, j, min;
            Console.WriteLine("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                Console.WriteLine();
            }
        }
    }
      
    // This code is contributed by Rajput-Ji


    C++
    #include 
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        for (i = N - 1; i >= 1; i--) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }


    Java
    // Java implementation of the approach
    class GFG 
    {
    public static void main(String[] args) 
    {
        int N = 4, i, j, min;
        System.out.println("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
        }
    }
      
    // This code is contributed by 29AjayKumar


    Python3
    # Python3 implementation of the approach
    N = 4
    print("Value of N: ", N) 
      
    for i in range(1, N + 1): 
        for j in range(1, N + 1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ") 
      
        for j in range(N - 1, 0, -1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ")
      
        print()
          
    for i in range(N - 1, 0, -1): 
        for j in range(1, N + 1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ") 
      
        for j in range(N - 1, 0, -1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ")
      
        print()
          
    # This code is contributed by sai prakash


    C#
    // C# implementation of the approach
    using System;
      
    class GFG 
    {
    public static void Main(String[] args) 
    {
        int N = 4, i, j, min;
        Console.WriteLine("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
        }
    }
      
    // This code is contributed by Rajput-Ji


    输出:
    Value of N: 4
    4444
    4333
    4322
    4321
    

    如果图案没有任何对称性,并且现在完成了,那么工作就完成了。但是对于对称的图案,它们仍然不完整,需要执行步骤4。

  • 步骤4:现在,在图片中包括图案的B部分,并将这些元素与列号相关联。但是不要按顺序分配列号,而是给它们分配与A部分中为其镜像列分配的列号相同的列号。

  • 步骤5:现在更改代码以添加B部分。为此,只需重新运行j = n-1到j = 1的内部循环。

    C++

    #include 
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }
    

    Java

    class GFG 
    {
        public static void main(String[] args) 
        {
            int N = 4, i, j, min;
            System.out.println("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                System.out.println();
            }
        }
    }
      
    // This code is contributed by PrinciRaj1992
    

    Python3

    N = 4;
    print("Value of N: ", N);
      
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            min = i if i < j else j;
            print(N - min + 1, end = "");
      
        for j in range(N - 1, 0, -1):
            min = i if i < j else j;
            print(N - min + 1, end = "");
        print();
      
    # This code is contributed by Rajput-Ji
    

    C#

    using System;
      
    class GFG 
    {
        public static void Main(String[] args) 
        {
            int N = 4, i, j, min;
            Console.WriteLine("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                Console.WriteLine();
            }
        }
    }
      
    // This code is contributed by Rajput-Ji
    
    输出:
    Value of N: 4
    4444444
    4333334
    4322234
    4321234
    
  • 步骤6:现在,在图片中包括图案的C和D部分,并将这些元素与行号相关联,方法与在步骤4中分配列号的方式相同。

  • 步骤7:现在,以与步骤5中相同的方式,更改代码以追加C和D部分。为此,只需重新运行外循环,即可将i = n-1转换为i = 1。

    C++

    #include 
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        for (i = N - 1; i >= 1; i--) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }
    

    Java

    // Java implementation of the approach
    class GFG 
    {
    public static void main(String[] args) 
    {
        int N = 4, i, j, min;
        System.out.println("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
        }
    }
      
    // This code is contributed by 29AjayKumar
    

    Python3

    # Python3 implementation of the approach
    N = 4
    print("Value of N: ", N) 
      
    for i in range(1, N + 1): 
        for j in range(1, N + 1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ") 
      
        for j in range(N - 1, 0, -1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ")
      
        print()
          
    for i in range(N - 1, 0, -1): 
        for j in range(1, N + 1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ") 
      
        for j in range(N - 1, 0, -1): 
            min = i if i < j else j 
            print(N - min + 1, end = " ")
      
        print()
          
    # This code is contributed by sai prakash
    

    C#

    // C# implementation of the approach
    using System;
      
    class GFG 
    {
    public static void Main(String[] args) 
    {
        int N = 4, i, j, min;
        Console.WriteLine("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
        }
    }
      
    // This code is contributed by Rajput-Ji
    
    输出:
    Value of N: 4
    4444444
    4333334
    4322234
    4321234
    4322234
    4333334
    4444444