📜  创建一个 nxn 方阵,其中所有子矩阵的对角元素之和为偶数

📅  最后修改于: 2022-05-13 01:54:21.822000             🧑  作者: Mango

创建一个 nxn 方阵,其中所有子矩阵的对角元素之和为偶数

给定一个整数 N。任务是生成(nxn)方阵,其元素范围为 1 到 n^2,条件如下:

  • 矩阵的元素应该是不同的,即只使用一次
  • 范围从 1 到 n^2 的数字
  • 您选择的每个子矩阵都应该具有对角元素的总和,即左上角和右下角的总和应该是偶数,右上角和左下角元素的总和应该是偶数

此属性应适用于矩阵的所有子矩阵。您需要生成一个偶数子矩阵

例子:

方法:

正如我们所知,任何两个元素的总和是偶数,它可以是奇数和奇数的总和或偶数和偶数的总和。在这两种情况下,角元素总和为偶数,我们需要确保对角线图案排列的元素应该是奇数或偶数。因此,我们使对角线为所有奇数或所有偶数的二维数组,以找到任何具有角元素总和的子矩阵。可以遵循以下方法。

  • 当 n 为奇数时,对角线已经在所有奇数或偶数元素中,因此我们无需修改并生成简单的二维数组
  • 当 n 为偶数时,生成的矩阵不满足子矩阵的对角元素之和为偶数的性质,因此我们反转交替的行元素,使得每个子矩阵的对角线要么全为奇数,要么全为偶数。

下面是实现。

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
void sub_mat_even(int N)
{
  // Counter to initialize
  // the values in 2-D array
  int K = 1;
   
  // To create a 2-D array
  // from to 1 to N*2
  int A[N][N];
   
  for(int i = 0; i < N; i++)
  {
    for(int j = 0; j < N; j++)
    {
      A[i][j] = K;
      K++;
    }
  }
 
  // If found even we reverse
  // the alternate row elements
  // to get all diagonal elements
  // as all even or all odd
  if(N % 2 == 0)
  {
    for(int i = 0; i < N; i++)
    {
      if(i % 2 == 1)
      {
        int s = 0;
        int l = N - 1;
         
        // Reverse the row
        while(s < l)
        {
          swap(A[i][s],
               A[i][l]);
          s++;
          l--;
        }
      }
    }
  }
 
  // Print the formed array
  for(int i = 0; i < N; i++)
  {
    for(int j = 0; j < N; j++)
    {
      cout << A[i][j] << " ";
    }
    cout << endl;
  }
}
 
// Driver code
int main()
{
    int N = 4;
   
    // Function call
    sub_mat_even(N);
}
 
// This code is contributed by mishrapriyanshu557


Java
// Java program for
// the above approach
import java.io.*;
 
class GFG{
 
static void sub_mat_even(int N)
{
     
    // Counter to initialize
    // the values in 2-D array
    int K = 1;
 
    // To create a 2-D array
    // from to 1 to N*2
    int[][] A = new int[N][N];
 
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            A[i][j] = K;
            K++;
        }
    }
 
    // If found even we reverse
    // the alternate row elements
    // to get all diagonal elements
    // as all even or all odd
    if (N % 2 == 0)
    {
        for(int i = 0; i < N; i++)
        {
            if (i % 2 == 1)
            {
                int s = 0;
                int l = N - 1;
 
                // Reverse the row
                while (s < l)
                {
                    swap(A[i], s, l);
                    s++;
                    l--;
                }
            }
        }
    }
 
    // Print the formed array
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            System.out.print(A[i][j] + " ");
        }
        System.out.println();
    }
}
 
private static void swap(int[] A, int s, int l)
{
    int temp = A[s];
    A[s] = A[l];
    A[l] = temp;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 4;
 
    // Function call
    sub_mat_even(N);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program for
# the above approach
import itertools
 
 
def sub_mat_even(n):
     
    temp = itertools.count(1)
     
    # create a 2d array ranging
    # from 1 to n^2
    l = [[next(temp)for i in range(n)]for i in range(n)]
     
    # If found even we reverse the alternate
    # row elements to get all diagonal elements
    # as all even or all odd
    if n%2 == 0:
        for i in range(0,len(l)):
            if i%2 == 1:
                l[i][:] = l[i][::-1]
     
    # Printing the array formed
    for i in range(n):
        for j in range(n):
            print(l[i][j],end=" ")
        print()
 
n = 4
sub_mat_even(n)


C#
// C# program for
// the above approach
using System;
class GFG {
     
    static void sub_mat_even(int N)
    {
          
        // Counter to initialize
        // the values in 2-D array
        int K = 1;
      
        // To create a 2-D array
        // from to 1 to N*2
        int[,] A = new int[N, N];
      
        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < N; j++)
            {
                A[i, j] = K;
                K++;
            }
        }
      
        // If found even we reverse
        // the alternate row elements
        // to get all diagonal elements
        // as all even or all odd
        if (N % 2 == 0)
        {
            for(int i = 0; i < N; i++)
            {
                if (i % 2 == 1)
                {
                    int s = 0;
                    int l = N - 1;
      
                    // Reverse the row
                    while (s < l)
                    {
                        int temp = A[i, s];
                        A[i, s] = A[i, l];
                        A[i, l] = temp;
                        s++;
                        l--;
                    }
                }
            }
        }
      
        // Print the formed array
        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < N; j++)
            {
                Console.Write(A[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
 
  static void Main() {
       
    int N = 4;
  
    // Function call
    sub_mat_even(N);
  }
}
 
// This code is contributed by divyeshrabadiya07


输出:

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

这种方法需要 O(n*2) 时间复杂度。