📜  生成一个矩阵,在每个2 x 2个子矩阵中所有对角线的和为偶数

📅  最后修改于: 2021-05-06 09:56:30             🧑  作者: Mango

给定一个正整数N ,任务是构造一个大小为N * N的矩阵,以使所有矩阵元素都与范围[1,N 2 ]和每2 * 2个子矩阵的对角线中的元素之和不同。甚至。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个矩阵,例如mat [] [] ,以存储矩阵元素,使得所有矩阵元素都不同于范围[1,N 2 ],并且每2 * 2个子矩阵的对角线中的矩阵元素之和为甚至。
  • 初始化一个变量,例如odd = 1 ,以存储奇数。
  • 初始化一个变量,例如even = 2 ,以存储偶数。
  • 通过检查以下条件来填充所有矩阵元素mat [i] [j]
    • 如果(i + j)%2 = 0 ,则设置mat [i] [j] =奇数并更新奇数+ = 2
    • 否则,将mat [i] [j]设置为偶数并更新偶数== 2
  • 最后,打印矩阵mat [] []

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to construct a matrix such that
// the sum elements in both diagonals of
// every 2 * 2 matrices is even
void generateMatrix(int N)
{
    // Stores odd numbers
    int odd = 1;
 
    // Stores even numbers
    int even = 2;
 
    // Store matrix elements such that
    // sum of elements in both diagonals
    // of every 2 * 2 submatrices is even
    int mat[N + 1][N + 1];
 
    // Fill all the values of
    // matrix elements
    for (int i = 1; i <= N; i++) {
 
        for (int j = 1; j <= N; j++) {
 
            if ((i + j) % 2 == 0) {
 
                mat[i][j] = odd;
 
                // Update odd
                odd += 2;
            }
 
            else {
 
                mat[i][j] = even;
 
                // Update even
                even += 2;
            }
        }
    }
 
    // Print the matrix
    for (int i = 1; i <= N; i++) {
 
        for (int j = 1; j <= N; j++) {
 
            cout << mat[i][j] << " ";
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int N = 4;
    generateMatrix(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to construct a matrix such that
// the sum elements in both diagonals of
// every 2 * 2 matrices is even
static void generateMatrix(int N)
{
     
    // Stores odd numbers
    int odd = 1;
 
    // Stores even numbers
    int even = 2;
 
    // Store matrix elements such that
    // sum of elements in both diagonals
    // of every 2 * 2 submatrices is even
    int[][] mat = new int[N + 1][N + 1];
 
    // Fill all the values of
    // matrix elements
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if ((i + j) % 2 == 0)
            {
                mat[i][j] = odd;
                 
                // Update odd
                odd += 2;
            }
 
            else
            {
                mat[i][j] = even;
                 
                // Update even
                even += 2;
            }
        }
    }
 
    // Print the matrix
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            System.out.print(mat[i][j] + " ");
        }
         
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
     
    generateMatrix(N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to construct a matrix such that
# the sum elements in both diagonals of
# every 2 * 2 matrices is even
def generateMatrix(N):
   
    # Stores odd numbers
    odd = 1;
 
    # Stores even numbers
    even = 2;
 
    # Store matrix elements such that
    # sum of elements in both diagonals
    # of every 2 * 2 submatrices is even
    mat = [[0 for i in range(N + 1)] for j in range(N + 1)] ;
 
    # Fill all the values of
    # matrix elements
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            if ((i + j) % 2 == 0):
                mat[i][j] = odd;
 
                # Update odd
                odd += 2;
            else:
                mat[i][j] = even;
 
                # Update even
                even += 2;
 
    # Prthe matrix
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            print(mat[i][j], end = " ");
        print();
 
# Driver Code
if __name__ == '__main__':
    N = 4;
    generateMatrix(N);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to construct a matrix such that
// the sum elements in both diagonals of
// every 2 * 2 matrices is even
static void generateMatrix(int N)
{
     
    // Stores odd numbers
    int odd = 1;
 
    // Stores even numbers
    int even = 2;
 
    // Store matrix elements such that
    // sum of elements in both diagonals
    // of every 2 * 2 submatrices is even
    int[,] mat = new int[N + 1, N + 1];
 
    // Fill all the values of
    // matrix elements
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if ((i + j) % 2 == 0)
            {
                mat[i, j] = odd;
                 
                // Update odd
                odd += 2;
            }
 
            else
            {
                mat[i, j] = even;
 
                // Update even
                even += 2;
            }
        }
    }
 
    // Print the matrix
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            Console.Write(mat[i, j] + " ");
        }
 
        Console.WriteLine();
    }
}
 
// Driver Code
static public void Main()
{
    int N = 4;
     
    generateMatrix(N);
}
}
 
// This code is contributed by Dharanendra L V


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

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