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

📅  最后修改于: 2021-09-06 05:29:04             🧑  作者: 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] =odd并更新odd += 2
    • 否则,设置mat[i][j] = even并更新even += 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


Javascript


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

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

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