📌  相关文章
📜  除一项外的矩阵和

📅  最后修改于: 2022-05-13 01:57:23.092000             🧑  作者: Mango

除一项外的矩阵和

给定 an*m 矩阵,找出矩阵元素的总和,其中没有由其位置指定的元素
例子:

Input : mat[] = {{1 2 4}, 
                {5 6 8}}
         cell = (0 2)
Output :22
We need to find sum of all elements
except mat[0][2].

Input : mat[][] = {{5 6 2 3}, {2 3 1 8}, {9 6 5 2}}
        cell = (1 2)
Output :51

一个简单的解决方案是遍历矩阵。对于每个访问过的单元格,检查它是否是要忽略的单元格。

C++
// C++ program to find sum
// of matrix except cell (x, y)
#include
using namespace std;
 
// Dimension of input matrix
# define R 2
# define C 3
 
// Returns sum of arr[][]
// except cell arr[x][y]
int calcSum(int arr[R][C],
            int x, int y)
{
    int sum = 0;
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
        {
            if (i != x || j != y)
                sum = sum + arr[i][j];
        }
    }
    return sum;
}
 
// Driver code
int main()
{
    int x = 0, y = 2;
    int arr[R][C] = {1, 2, 4,
                     5, 6, 8 };
                     
    cout << calcSum(arr, x, y);
}
 
// This code is contributed
// by ChitraNayal


Java
// Java program to find sum of matrix except
// cell (x, y)
class Matrix {
 
    // Returns sum of arr[][] except cell arr[x][y]
    static int calcSum(int[][] arr, int x, int y)
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
              if (i != x || j != y)
                sum = sum + arr[i][j];
            }
        }
        return sum;
    }
    public static void main(String[] args)
    {
        int x = 0, y = 2;
        int[][] arr = {
            { 1, 2, 4 }, { 5, 6, 8 },
        };
        System.out.println(calcSum(arr, x, y));
    }
}


C#
// C# program to find sum
// of matrix except cell (x, y)
using System;
 
class GFG
{
 
    // Returns sum of arr[,]
    // except cell arr[x][y]
    static int calcSum(int[,] arr,
                       int x, int y)
    {
        int sum = 0;
        for (int i = 0;
                 i < arr.GetLength(0); i++)
        {
            for (int j = 0;
                     j < arr.GetLength(1); j++)
            {
            if (i != x || j != y)
                sum = sum + arr[i, j];
            }
        }
        return sum;
    }
     
    // Driver Code
    public static void Main()
    {
        int x = 0, y = 2;
        int[,] arr = {{ 1, 2, 4 },
                      { 5, 6, 8 }};
        Console.Write(calcSum(arr, x, y));
    }
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to find
# sum of matrix except cell (x, y)
 
# Returns sum of arr[][]
# except cell arr[x][y]
def calcSum(arr, x, y):
    s = 0
    for i in range(R):
        for j in range(C):
            if (i != x or j != y):
                s = s + arr[i][j];
    return s;
 
# Driver code
x = 0
y = 2
arr = [[ 1, 2, 4 ],
       [ 5, 6, 8 ]]
R = 2
C = 3
 
print(calcSum(arr, x, y))
 
# This code is contributed
# by ChitraNayal


PHP


Javascript


C++
// C++ program to find sum
// of matrix except cell (x, y)
#include
using namespace std;
 
#define R 2
#define C 3
 
// Returns sum of arr[][]
// except cell arr[x][y]
int calcSum(int arr[R][C],
            int x, int y)
{
    int sum = 0;
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            sum = sum + arr[i][j];
 
    return sum - arr[x][y];
}
 
// Driver code
int main()
{
    int x = 0, y = 2;
    int arr[R][C]= {1, 2, 4 ,
                    5, 6, 8 };
 
    cout << (calcSum(arr, x, y));
}
 
// This code is contributed
// by ChitraNayal


Java
// Java program to find sum of matrix except
// cell (x, y)
class Matrix {
 
    // Returns sum of arr[][] except cell arr[x][y]
    static int calcSum(int[][] arr, int x, int y)
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
            for (int j = 0; j < arr[i].length; j++)
                sum = sum + arr[i][j];
 
        return sum - arr[x][y];
    }
 
    public static void main(String[] args)
    {
        int x = 0, y = 2;
        int[][] arr = {
            { 1, 2, 4 }, { 5, 6, 8 },
        };
        System.out.println(calcSum(arr, x, y));
    }
}


C#
// C# program to find sum
// of matrix except cell (x, y)
using System;
 
class GFG
{
 
    // Returns sum of arr[,]
    // except cell arr[x,y]
    static int calcSum(int[,] arr,
                       int x, int y)
    {
        int sum = 0;
        for (int i = 0;
                 i < arr.GetLength(0); i++)
            for (int j = 0;
                     j < arr.GetLength(1); j++)
                sum = sum + arr[i, j];
 
        return sum - arr[x, y];
    }
 
    // Driver code
    public static void Main()
    {
        int x = 0, y = 2;
        int[,] arr = {{ 1, 2, 4 },
                      { 5, 6, 8 }};
        Console.Write(calcSum(arr, x, y));
    }
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to find
# sum of matrix except cell (x, y)
 
# Returns sum of arr[][]
# except cell arr[x][y]
def calcSum( arr, x, y):
    s = 0
    for i in range(R):
        for j in range(C):
            s = s + arr[i][j]
 
    return s - arr[x][y]
 
# Driver code
x = 0
y = 2
R = 2
C = 3
arr = [[1, 2, 4 ],
       [5, 6, 8 ]]
 
print (calcSum(arr, x, y))
 
# This code is contributed
# by ChitraNayal


PHP


Javascript


输出:
22

上述解决方案会导致对每个矩阵项进行额外的比较。更好的解决方案是首先找到总和,然后减去给定的单元格。

C++

// C++ program to find sum
// of matrix except cell (x, y)
#include
using namespace std;
 
#define R 2
#define C 3
 
// Returns sum of arr[][]
// except cell arr[x][y]
int calcSum(int arr[R][C],
            int x, int y)
{
    int sum = 0;
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            sum = sum + arr[i][j];
 
    return sum - arr[x][y];
}
 
// Driver code
int main()
{
    int x = 0, y = 2;
    int arr[R][C]= {1, 2, 4 ,
                    5, 6, 8 };
 
    cout << (calcSum(arr, x, y));
}
 
// This code is contributed
// by ChitraNayal

Java

// Java program to find sum of matrix except
// cell (x, y)
class Matrix {
 
    // Returns sum of arr[][] except cell arr[x][y]
    static int calcSum(int[][] arr, int x, int y)
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
            for (int j = 0; j < arr[i].length; j++)
                sum = sum + arr[i][j];
 
        return sum - arr[x][y];
    }
 
    public static void main(String[] args)
    {
        int x = 0, y = 2;
        int[][] arr = {
            { 1, 2, 4 }, { 5, 6, 8 },
        };
        System.out.println(calcSum(arr, x, y));
    }
}

C#

// C# program to find sum
// of matrix except cell (x, y)
using System;
 
class GFG
{
 
    // Returns sum of arr[,]
    // except cell arr[x,y]
    static int calcSum(int[,] arr,
                       int x, int y)
    {
        int sum = 0;
        for (int i = 0;
                 i < arr.GetLength(0); i++)
            for (int j = 0;
                     j < arr.GetLength(1); j++)
                sum = sum + arr[i, j];
 
        return sum - arr[x, y];
    }
 
    // Driver code
    public static void Main()
    {
        int x = 0, y = 2;
        int[,] arr = {{ 1, 2, 4 },
                      { 5, 6, 8 }};
        Console.Write(calcSum(arr, x, y));
    }
}
 
// This code is contributed
// by ChitraNayal

Python3

# Python 3 program to find
# sum of matrix except cell (x, y)
 
# Returns sum of arr[][]
# except cell arr[x][y]
def calcSum( arr, x, y):
    s = 0
    for i in range(R):
        for j in range(C):
            s = s + arr[i][j]
 
    return s - arr[x][y]
 
# Driver code
x = 0
y = 2
R = 2
C = 3
arr = [[1, 2, 4 ],
       [5, 6, 8 ]]
 
print (calcSum(arr, x, y))
 
# This code is contributed
# by ChitraNayal

PHP


Javascript


输出:
22