📌  相关文章
📜  构造一个总和等于对角元素总和的矩阵

📅  最后修改于: 2021-10-26 05:10:46             🧑  作者: Mango

给定一个整数N ,任务是使用正整数和负整数构造一个大小为 N 2且不包括0的矩阵,使得矩阵的和等于矩阵的对角线的和。
例子:

方法:
解决问题的方法是遍历矩阵的所有索引并在N 个对角线位置打印一个正元素(比如y ),并在剩余的N 个中平均分配一个单值正负整数(比如x-x2 – N 个位置。
下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to construct matrix with
// diagonal sum equal to matrix sum
void constructmatrix(int N)
{
    bool check = true;
 
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < N; j++) {
 
            // If diagonal position
            if (i == j) {
                cout << 1 << " ";
            }
 
            else if (check) {
 
                // Positve element
                cout << 2 << " ";
                check = false;
            }
            else {
 
                // Negative element
                cout << -2 << " ";
                check = true;
            }
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int N = 5;
 
    constructmatrix(5);
 
    return 0;
}


Java
// Java program to implement
// the above approach
public class Main {
 
    // Function to construct matrix with
    // diagonal sum equal to matrix sum
    public static void constructmatrix(int N)
    {
        boolean check = true;
 
        for (int i = 0; i < N; i++) {
 
            for (int j = 0; j < N; j++) {
 
                // If diagonal position
                if (i == j) {
                    System.out.print("1 ");
                }
                else if (check) {
 
                    // Positve element
                    System.out.print("2 ");
                    check = false;
                }
                else {
                    // Negative element
                    System.out.print("-2 ");
                    check = true;
                }
            }
 
            System.out.println();
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
 
        constructmatrix(5);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to construct matrix with
# diagonal sum equal to matrix sum
def constructmatrix(N):
     
    check = bool(True)
 
    for i in range(N):
        for j in range(N):
 
            # If diagonal position
            if (i == j):
                print(1, end = " ")
 
            elif (check):
 
                # Positve element
                print(2, end = " ")
                check = bool(False)
                 
            else:
 
                # Negative element
                print(-2, end = " ")
                check = bool(True)
 
        print()
 
# Driver code
N = 5
constructmatrix(5)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to construct matrix with
// diagonal sum equal to matrix sum
public static void constructmatrix(int N)
{
    bool check = true;
 
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // If diagonal position
            if (i == j)
            {
                Console.Write("1 ");
            }
            else if (check)
            {
                 
                // Positve element
                Console.Write("2 ");
                check = false;
            }
            else
            {
                 
                // Negative element
                Console.Write("-2 ");
                check = true;
            }
        }
        Console.WriteLine();
    }
}
 
// Driver Code
static public void Main ()
{
    int N = 5;
 
    constructmatrix(N);
}
}
 
// This code is contributed by piyush3010


Javascript


输出:
1 2 -2 2 -2 
2 1 -2 2 -2 
2 -2 1 2 -2 
2 -2 2 1 -2 
2 -2 2 -2 1

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