📜  在Zig-Zag矩阵中按升序排列数字1到N ^ 2

📅  最后修改于: 2021-04-24 18:58:24             🧑  作者: Mango

给定正整数N ,任务是打印由1到N 2的数字组成的N×N之字形矩阵,以使矩阵的ZigZag遍历产生按升序排列的数字。
例子:

方法:
所需的矩阵可以分解为两个直角三角形。

  • 倒置的直角三角形(视为上三角形)。
  • 一个正常的直角三角形(视为下三角形)。

这个想法是迭代两个嵌套的循环,用各自的值填充上三角。然后再次迭代两个嵌套循环,用相应的值填充下三角。完成上述两个操作后,将打印所需的矩阵。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the pattern
void printPattern(int n)
{
    // N * N matrix to store the
    // values
    int arr[n][n];
 
    arr[0][0] = 1;
 
    // Fill the values of
    // upper triangle
    for (int i = 0; i < n; i++) {
 
        if (i > 0) {
            arr[i][0] = arr[i - 1][0] + i + 1;
        }
        for (int j = 1;
             j < n - i; j++) {
 
            arr[i][j] = arr[i][j - 1] + i + j;
        }
    }
 
    // Fill the values of
    // lower triangle
    arr[1][n - 1] = arr[n - 1][0] + 1;
    int div = 0;
 
    for (int i = 2; i < n; i++) {
 
        div = n - 2;
        for (int j = n - i;
             j < n; j++) {
 
            if (j == n - i) {
                arr[i][j] = arr[i - 1][j + 1]
                            + 1;
            }
            else {
                arr[i][j] = arr[i][j - 1]
                            + div;
                div--;
            }
        }
    }
 
    // Print the array
    for (int i = 0; i < n; i++) {
 
        for (int j = 0; j < n; j++) {
 
            cout << arr[i][j] << " ";
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    // Given size of matrix
    int N = 4;
 
    // Function Call
    printPattern(N);
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to print the pattern
static void printPattern(int n)
{
  // N * N matrix to store the
  // values
  int [][]arr = new int[n][n];
 
  arr[0][0] = 1;
 
  // Fill the values of
  // upper triangle
  for (int i = 0; i < n; i++)
  {
    if (i > 0)
    {
      arr[i][0] = arr[i - 1][0] +
                      i + 1;
    }
    for (int j = 1; j < n - i; j++)
    {
      arr[i][j] = arr[i][j - 1] +
                      i + j;
    }
  }
 
  // Fill the values of
  // lower triangle
  arr[1][n - 1] = arr[n - 1][0] + 1;
  int div = 0;
 
  for (int i = 2; i < n; i++)
  {
    div = n - 2;
    for (int j = n - i; j < n; j++)
    {
      if (j == n - i)
      {
        arr[i][j] = arr[i - 1][j + 1] + 1;
      }
      else
      {
        arr[i][j] = arr[i][j - 1] + div;
        div--;
      }
    }
  }
 
  // Print the array
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      System.out.print(arr[i][j] + " ");
    }
    System.out.print("\n");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given size of matrix
  int N = 4;
 
  // Function Call
  printPattern(N);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
 
# Function to print the pattern
def printPattern(n):
         
        # N * N matrix to store the values
    arr = [[0 for i in range(n)]
        for j in range(n)]
 
    # Fill the values of upper triangle
    arr[0][0] = 1
    for i in range(n):
        if i > 0:
            arr[i][0] = arr[i - 1][0] + i + 1
        for j in range(1, n-i):
            arr[i][j] = arr[i][j - 1] + i + j
 
    # Fill the values of lower triangle
    if n > 1:
        arr[1][n - 1] = arr[n - 1][0] + 1
    div = 0
    for i in range(2, n):
        div = n-2
        for j in range(n-i, n):
            if j == n-i:
                arr[i][j] = arr[i - 1][j + 1] + 1
            else:
                arr[i][j] = arr[i][j - 1] + div
                div -= 1
 
    # Print the array
    for i in range(n):
        for j in range(n):
            print(arr[i][j], end=' ')
        print("")
 
 
# Driver code
# Given size of matrix
N = 4
 
# Function Call
printPattern(N)


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to print the pattern
static void printPattern(int n)
{
  // N * N matrix to store the
  // values
  int [,]arr = new int[n, n];
 
  arr[0,0] = 1;
 
  // Fill the values of
  // upper triangle
  for (int i = 0; i < n; i++)
  {
    if (i > 0)
    {
      arr[i, 0] = arr[i - 1, 0] +
                      i + 1;
    }
    for (int j = 1; j < n - i; j++)
    {
      arr[i, j] = arr[i, j - 1] +
                      i + j;
    }
  }
 
  // Fill the values of
  // lower triangle
  arr[1, n - 1] = arr[n - 1, 0] + 1;
  int div = 0;
 
  for (int i = 2; i < n; i++)
  {
    div = n - 2;
    for (int j = n - i; j < n; j++)
    {
      if (j == n - i)
      {
        arr[i, j] = arr[i - 1, j + 1] + 1;
      }
      else
      {
        arr[i, j] = arr[i, j - 1] + div;
        div--;
      }
    }
  }
 
  // Print the array
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Console.Write(arr[i, j] + " ");
    }
    Console.Write("\n");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given size of matrix
  int N = 4;
 
  // Function Call
  printPattern(N);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
1 2 4 7 
3 5 8 11 
6 9 12 14 
10 13 15 16 

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