📌  相关文章
📜  构造一个方阵,其对角线和的奇偶性与矩阵的大小相同

📅  最后修改于: 2021-09-07 02:58:43             🧑  作者: 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live