📜  查找矩阵的法线和迹线的程序

📅  最后修改于: 2021-04-29 03:33:55             🧑  作者: Mango

给定2D矩阵,任务是查找矩阵的“迹线”和“法线”。
矩阵的法线定义为矩阵元素平方和的平方根。
轴方阵的轨迹是对角元素的总和。
例子 :

Input : mat[][] = {{7, 8, 9},
                   {6, 1, 2},
                   {5, 4, 3}};
Output : Normal = 16  
         Trace  = 11
Explanation : 
Normal = sqrt(7*7+ 8*8 + 9*9 + 6*6 +
              1*1 + 2*2 + 5*5 + 4*4 + 3*3)   
       = 16
Trace  = 7+1+3 = 11

Input :mat[][] = {{1, 2, 3},
                  {6, 4, 5},
                  {2, 1, 3}};
Output : Normal = 10  
         Trace = 8
Explanation : 
Normal = sqrt(1*1 +2*2 + 3*3 + 6*6 + 4*4 + 
             5*5 + 2*2 + 1*1 + 3*3)   
Trace = 8(1+4+3)

无标题

C++
// C++ program to find trace and normal
// of given matrix
#include
using namespace std;
 
// Size of given matrix
const int MAX = 100;
 
// Returns Normal of a matrix of size n x n
int findNormal(int mat[][MAX], int n)
{
    int sum = 0;
    for (int i=0; iJava
// Java program to find trace and normal
// of given matrix
 
import java.io.*;
 
class GFG {
 
// Size of given matrix
static  int MAX = 100;
 
// Returns Normal of a matrix of size n x n
 static int findNormal(int mat[][], int n)
{
    int sum = 0;
    for (int i=0; iPython3
# Python3 program to find trace and
# normal of given matrix
import math
 
# Size of given matrix
MAX = 100;
 
# Returns Normal of a matrix
# of size n x n
def findNormal(mat, n):
 
    sum = 0;
    for i in range(n):
        for j in range(n):
            sum += mat[i][j] * mat[i][j];
    return math.floor(math.sqrt(sum));
 
# Returns trace of a matrix of
# size n x n
def findTrace(mat, n):
 
    sum = 0;
    for i in range(n):
        sum += mat[i][i];
    return sum;
 
# Driver Code
mat = [[1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5]];
 
print("Trace of Matrix =", findTrace(mat, 5));
 
print("Normal of Matrix =", findNormal(mat, 5));
 
# This code is contributed by mits


C#
// C# program to find trace and normal
// of given matrix
using System;
 
class GFG {
     
    // Returns Normal of a matrix of
    // size n x n
    static int findNormal(int [,]mat, int n)
    {
        int sum = 0;
         
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                sum += mat[i,j] * mat[i,j];
                 
        return (int)Math.Sqrt(sum);
    }
     
    // Returns trace of a matrix of size
    // n x n
    static int findTrace(int [,]mat, int n)
    {
        int sum = 0;
         
        for (int i = 0; i < n; i++)
            sum += mat[i,i];
             
        return sum;
    }
     
    // Driven source
    public static void Main ()
    {
        int [,]mat = { {1, 1, 1, 1, 1},
                       {2, 2, 2, 2, 2},
                       {3, 3, 3, 3, 3},
                       {4, 4, 4, 4, 4},
                       {5, 5, 5, 5, 5},
    };
 
    Console.Write ("Trace of Matrix = "
            + findTrace(mat, 5) + "\n");
    Console.Write("Normal of Matrix = "
                    + findNormal(mat, 5));
         
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :

Trace of matrix = 15
Normal of matrix = 16