📜  构造一个平方矩阵,其对角和的奇偶校验等于矩阵的大小

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

给定一个表示矩阵大小的整数N ,任务是构造一个正方形矩阵N * N ,该矩阵的元素从1到N 2 ,使得对角线之和的奇偶校验等于整数N的奇偶校验。

例子:

方法:这个想法是要观察到,以另一种方式填充矩阵中的元素时, N的奇偶校验和对角线的总和是相同的。从1开始计数器,然后从0到N – 1以递增的顺序填充第一行,然后从索引N – 1到0填充第二行,依此类推。继续以这种替代方式填充值从1到N 2的每个元素,以获取所需的矩阵。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to construct a N * N
// matrix based on the given condition
void createMatrix(int N)
{
    // Matrix with given sizM
    int M[N][N];
 
    // Counter to insert elements
    // from 1 to N * N
    int count = 1;
    for (int i = 0; i < N; i++) {
 
        // Check if it is first row
        // or odd row of the matrix
        if (i % 2 == 0) {
 
            for (int j = 0; j < N; j++) {
                M[i][j] = count;
                count++;
            }
        }
        else
        {
            // Insert elements from
            // right to left
            for (int j = N - 1;j >= 0; j--){
                M[i][j] = count;
                count += 1;
            }
        }
    }
 
    // Print the matrix
    for (int i = 0; i < N; i++) {
 
        // Traverse column
        for (int j = 0; j < N; j++) {
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given size of matrix N
    int N = 3;
 
    // Function Call
    createMatrix(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function to construct a N * N
// matrix based on the given condition
static void createMatrix(int N)
{
    // Matrix with given sizM
    int M[][] = new int[N][N];
  
    // Counter to insert elements
    // from 1 to N * N
    int count = 1;
    for (int i = 0; i < N; i++)
    {
  
        // Check if it is first row
        // or odd row of the matrix
        if (i % 2 == 0)
        {
  
            for (int j = 0; j < N; j++)
            {
                M[i][j] = count;
                count++;
            }
        }
        else
        {
            // Insert elements from
            // right to left
            for(int j = N - 1; j >= 0; j--){
                M[i][j] = count;
                count += 1 ;
            }
        }
    }
  
    // Print the matrix
    for (int i = 0; i < N; i++)
    {
  
        // Traverse column
        for (int j = 0; j < N; j++)
        {
            System.out.print(M[i][j] + " ");
        }
        System.out.println();
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given size of matrix N
    int N = 3;
  
    // Function Call
    createMatrix(N);
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program for the above approach
 
# Function to construct a N * N
# matrix based on the given condition
def createMatrix(N):
     
    # Matrix with given size
    M = [[0] * N for i in range(N)]
 
    # Counter to insert elements
    # from 1 to N * N
    count = 1
     
    for i in range(N):
 
        # Check if it is first row
        # or odd row of the matrix
        if (i % 2 == 0):
            for j in range(N):
 
                # Insert elements from
                # left to right
                M[i][j] = count
                count += 1
 
        # Condition if it is second
        # row or even row
        else:
 
            # Insert elements from
            # right to left
            for j in range(N - 1, -1, -1):
                M[i][j] = count
                count += 1
 
    # Print the matrix
    for i in range(N):
 
        # Traverse column
        for j in range(N):
            print(M[i][j], end = " ")
 
        print()
 
# Driver Code
if __name__ == '__main__':
     
    # Given size of matrix N
    N = 3
 
    # Function call
    createMatrix(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
class GFG{
  
// Function to construct a N * N
// matrix based on the given condition
static void createMatrix(int N)
{
  // Matrix with given sizM
  int[,] M = new int[N, N];
 
  // Counter to insert elements
  // from 1 to N * N
  int count = 1;
  for (int i = 0; i < N; i++)
  {
    // Check if it is first row
    // or odd row of the matrix
    if (i % 2 == 0)
    {
 
      for (int j = 0; j < N; j++)
      {
        M[i, j] = count;
        count++;
      }
    }
    else
    {
      // Insert elements from
      // right to left
      for(int j = N - 1; j >= 0; j--)
      {
        M[i, j] = count;
        count += 1;
      }
    }
  }
 
  // Print the matrix
  for (int i = 0; i < N; i++)
  {
    // Traverse column
    for (int j = 0; j < N; j++)
    {
      Console.Write(M[i, j] + " ");
    }
    Console.WriteLine();
  }
}
  
// Driver Code
public static void Main()
{
  // Given size of matrix N
  int N = 3;
 
  // Function Call
  createMatrix(N);
}
}
 
// This code is contributed by Chitranayal


Javascript


输出:
1 2 3 
6 5 4 
7 8 9

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