📌  相关文章
📜  计算主对角线的总和以及在方矩阵中包含重复值的行和列的数量

📅  最后修改于: 2021-04-18 02:38:06             🧑  作者: Mango

给定尺寸为N * N的矩阵M [] [] ,该矩阵仅由1N范围内的整数组成,任务是计算主对角线上存在的矩阵元素的总和,包含重复行和列的行数价值观。

例子:

方法:该方法是简单地遍历矩阵的所有元素,并找到对角线的总和以及具有重复值的行和列的数量。请按照以下步骤解决给定的问题:

  • 初始化变量tracerowRepeatcolumnRepeat分别存储主对角线的总和,行数和包含重复矩阵元素的列。
  • 遍历矩阵M [i] [j]中存在的每个元素,并在i等于j时增加总和迹线
  • 要查找包含重复值的行,请一次遍历一行,比较值,然后检查是否存在重复值。在获得第一对重复元素后,将rowRepeat递增1并退出循环。
  • 对矩阵的每一行重复上述步骤。
  • 对所有列重复相同的过程,如果值匹配,则columnRepeat会增加1
  • 完成所有迭代后,将tracerowRepeatcolumnRepeat的值打印为结果。

下面是上述方法的实现:

C++14
// C++14 program for the above approach
#include 
using namespace std;
 
// Function to calculate trace of
// a matrix and number of rows and
// columns with repeated elements
void vestigium(int N, int M[4][4])
{
     
    // Stores the trace, number of
    // rows and columns consisting
    // of repeated matrix elements
    int trace = 0, row_repeat = 0;
    int column_repeat = 0;
 
    // Iterate over the matrix
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // If current element is
            // present in the main diagonal
            if (i == j)
            {
                 
                // Update trace
                trace += M[i][j];
            }
        }
 
        int flag1 = 0;
 
        // Iterate over each row
        // and increment row_repeat
        // if repeated values exists
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // For each valid range
                if (j != k && M[i][j] == M[i][k])
                {
                    row_repeat++;
                    flag1 = 1;
                    break;
                }
            }
 
            if (flag1 == 1)
            {
                break;
            }
        }
 
        int flag2 = 0;
 
        // Iterate over each column and
        // increment column_repeat if
        // repeated values are encountered
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // For each valid range
                if (j != k && M[j][i] == M[k][i])
                {
                    column_repeat++;
                    flag2 = 1;
                    break;
                }
            }
 
            if (flag2 == 1)
            {
                break;
            }
        }
    }
 
    // Answer
    cout << trace << " "
         << row_repeat << " "
         << column_repeat ;
}
 
// Driver Code
int main()
{
    int M[4][4] = { { 1, 2, 3, 4 },
                    { 2, 1, 4, 3 },
                    { 3, 4, 1, 2 },
                    { 4, 3, 2, 1 } };
                       
    int N = sizeof(M) / sizeof(M[0]);
 
    vestigium(N, M);
}
 
// This code is contributed by sanjoy_62


Java
// Java program for the above approach
public class GFG {
 
    // Function to calculate trace of
    // a matrix and number of rows and
    // columns with repeated elements
    public static String vestigium(
        int N, int M[][])
    {
        // Stores the trace, number of
        // rows and columns consisting
        // of repeated matrix elements
        int trace = 0, row_repeat = 0;
        int column_repeat = 0;
 
        // Iterate over the matrix
        for (int i = 0; i < N; i++) {
 
            for (int j = 0; j < N; j++) {
 
                // If current element is
                // present in the main diagonal
                if (i == j) {
 
                    // Update trace
                    trace += M[i][j];
                }
            }
 
            int flag1 = 0;
 
            // Iterate over each row
            // and increment row_repeat
            // if repeated values exists
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) {
 
                    // For each valid range
                    if (j != k
                        && M[i][j] == M[i][k]) {
                        row_repeat++;
                        flag1 = 1;
                        break;
                    }
                }
 
                if (flag1 == 1) {
 
                    break;
                }
            }
 
            int flag2 = 0;
 
            // Iterate over each column and
            // increment column_repeat if
            // repeated values are encountered
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) {
 
                    // For each valid range
                    if (j != k
                        && M[j][i] == M[k][i]) {
 
                        column_repeat++;
                        flag2 = 1;
                        break;
                    }
                }
 
                if (flag2 == 1) {
 
                    break;
                }
            }
        }
 
        // Answer
        String output = trace + " "
                        + row_repeat + " "
                        + column_repeat + "\n";
 
        // Return answer
        return output;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int M[][] = { { 1, 2, 3, 4 },
                      { 2, 1, 4, 3 },
                      { 3, 4, 1, 2 },
                      { 4, 3, 2, 1 } };
        int N = M.length;
 
        String output = vestigium(N, M);
 
        // Print the output
        System.out.print(output);
    }
}


Python3
# Python3 program for the above approach
 
# Function to calculate trace of
# a matrix and number of rows and
# columns with repeated elements
def vestigium(N, M) :
     
    # Stores the trace, number of
    # rows and columns consisting
    # of repeated matrix elements
    trace = 0
    row_repeat = 0
    column_repeat = 0
 
    # Iterate over the matrix
    for i in range(N) :
        for j in range(N) :
             
            # If current element is
            # present in the main diagonal
            if (i == j):
                 
                # Update trace
                trace += M[i][j]
        flag1 = 0
 
        # Iterate over each row
        # and increment row_repeat
        # if repeated values exists
        for j in range(N) :
            for k in range(N) :
                 
                # For each valid range
                if (j != k and M[i][j] == M[i][k]) :
                    row_repeat += 1
                    flag1 = 1
                    break
            if (flag1 == 1) :
                break
        flag2 = 0
 
        # Iterate over each column and
        # increment column_repeat if
        # repeated values are encountered
        for j in range(N) :
            for k in range(N) :
                 
                # For each valid range
                if (j != k and M[j][i] == M[k][i]) :
                    column_repeat += 1
                    flag2 = 1
                    break
                 
            if (flag2 == 1) :
                break
 
    # Answer
    print(trace, row_repeat, column_repeat)
 
# Driver Code
M = [[ 1, 2, 3, 4 ],
    [ 2, 1, 4, 3 ],
    [ 3, 4, 1, 2 ],
    [ 4, 3, 2, 1 ]]           
N = len(M)
vestigium(N, M)
 
# This code is contributed by avijitmonald1998.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate trace of
// a matrix and number of rows and
// columns with repeated elements
public static String vestigium(int N, int[,] M)
{
     
    // Stores the trace, number of
    // rows and columns consisting
    // of repeated matrix elements
    int trace = 0, row_repeat = 0;
    int column_repeat = 0;
 
    // Iterate over the matrix
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // If current element is
            // present in the main diagonal
            if (i == j)
            {
                 
                // Update trace
                trace += M[i, j];
            }
        }
 
        int flag1 = 0;
 
        // Iterate over each row
        // and increment row_repeat
        // if repeated values exists
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // For each valid range
                if (j != k && M[i, j] == M[i, k])
                {
                    row_repeat++;
                    flag1 = 1;
                    break;
                }
            }
 
            if (flag1 == 1)
            {
                break;
            }
        }
 
        int flag2 = 0;
 
        // Iterate over each column and
        // increment column_repeat if
        // repeated values are encountered
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // For each valid range
                if (j != k && M[j, i] == M[k, i])
                {
                    column_repeat++;
                    flag2 = 1;
                    break;
                }
            }
 
            if (flag2 == 1)
            {
                break;
            }
        }
    }
 
    // Answer
    string output = trace + " " + row_repeat + " " +
                    column_repeat + "\n";
 
    // Return answer
    return output;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[, ] M = { { 1, 2, 3, 4 },
                  { 2, 1, 4, 3 },
                  { 3, 4, 1, 2 },
                  { 4, 3, 2, 1 } };
    int N = M.GetLength(0);
 
    string output = vestigium(N, M);
 
    // Print the output
    Console.Write(output);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
4 0 0

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