检查给定矩阵是否为零除矩阵
给定一个NxN方阵M[][] 。任务是检查矩阵M是否是零除矩阵。仅当列元素乘积与行元素乘积的2 个或多个下除法结果为零时,才称矩阵为零除法矩阵。
例子:
Input: M[ ][ ] = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ]
Output: 1
Explanation: Refer to image for explanation.
Input: M[ ][ ] = [ [ 1, 2, 3 ],
[ 6, 8, 2 ],
[ 7, 8, 9 ] ]
Output: 0
方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。
- 取一个矩阵M[ ][ ]并计算逐行和逐列元素的乘积。
- 将每行和每列的乘积分别存储在R[ ]和C[ ]数组中。
- 现在将C[ ]数组的索引元素除以R[ ]数组。
- 如果我们得到两个或多个零,则它是零除网格并返回1否则返回0 。
- 如果分母为零,则为零除法错误返回-1 。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to check zero division matrix
int ZeroDiv(vector >& M, int& N)
{
// R and C array to store product
vector R, C;
// Iterate through the matrix
// to take product
for (int i = 0; i < N; i++) {
int r = 1;
int c = 1;
for (int j = 0; j < N; j++) {
r *= M[i][j];
c *= M[j][i];
}
// Appending product of each row
// and column to R and C array.
R.push_back(r);
C.push_back(c);
}
// z to count number of zero
int z = 0;
// Try block to check zero division error
try {
for (int i = 0; i < N; i++)
if (C[i] / R[i] == 0)
z += 1;
}
// If zero division error occur return -1
catch (int x) {
cout << x << "\n";
return -1;
}
// If z greater than equal to 2 return 1
if (z >= 2) {
return 1;
}
// Else return 0
else {
return 0;
}
}
// Driver Code
int main()
{
// # Matrix M
vector > M
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// N length of matrix M
int N = M.size();
// Call zerodiv function
int x = ZeroDiv(M, N);
// Print the answer
cout << x << "\n";
}
// This code is contributed by Taranpreet
Java
// JAVA program for above approach
import java.util.*;
class GFG
{
// Function to check zero division matrix
public static int
ZeroDiv(ArrayList > M, int N)
{
// R and C array to store product
ArrayList R = new ArrayList();
ArrayList C = new ArrayList();
// Iterate through the matrix
// to take product
for (int i = 0; i < N; i++) {
int r = 1;
int c = 1;
for (int j = 0; j < N; j++) {
r = r * M.get(i).get(j);
c = c * M.get(i).get(j);
}
// Appending product of each row
// and column to R and C array.
R.add(r);
C.add(c);
}
// z to count number of zero
int z = 0;
// Try block to check zero division error
try {
for (int i = 0; i < N; i++)
if (C.get(i) % R.get(i) == 0)
z = z + 1;
}
// If zero division error occur return -1
catch (Exception e) {
System.out.println("Something went wrong" + e);
return -1;
}
// If z greater than equal to 2 return 1
if (z >= 2) {
return 1;
}
// Else return 0
else {
return 0;
}
}
// Driver Code
public static void main(String[] args)
{
// # Matrix M
ArrayList > M
= new ArrayList >();
ArrayList temp1
= new ArrayList<>(Arrays.asList(1, 2, 3));
ArrayList temp2
= new ArrayList<>(Arrays.asList(4, 5, 6));
ArrayList temp3
= new ArrayList<>(Arrays.asList(7, 8, 9));
M.add(temp1);
M.add(temp2);
M.add(temp3);
// N length of matrix M
int N = M.size();
// Call zerodiv function
int x = ZeroDiv(M, N);
// Print the answer
System.out.println(x);
}
}
// This code is contributed by Taranpreet
Python3
# Python program for above approach
# Function to check zero division matrix
def ZeroDiv(M, N):
# R and C array to store product
R = []
C = []
# Iterate through the matrix
# to take product
for i in range(N):
r = 1
c = 1
for j in range(N):
r *= M[i][j]
c *= M[j][i]
# Appending product of each row
# and column to R and C array.
R.append(r)
C.append(c)
# z to count number of zero
z = 0
# Try block to check zero division error
try:
for i in range(N):
if C[i]//R[i] == 0:
z += 1
# If zero division error occur return -1
except:
return -1
# If z greater than equal to 2 return 1
if z >= 2:
return 1
# Else return 0
else:
return 0
# Driver Code
if __name__ == "__main__":
# Matrix M
M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# N length of matrix M
N = len(M)
# Call zerodiv function
x = ZeroDiv(M, N)
# Print the answer
print(x)
C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to check zero division matrix
public static int
ZeroDiv(List > M, int N)
{
// R and C array to store product
List R = new List();
List C = new List();
// Iterate through the matrix
// to take product
for (int i = 0; i < N; i++) {
int r = 1;
int c = 1;
for (int j = 0; j < N; j++) {
r = r * M[i][j];
c = c * M[i][j];
}
// Appending product of each row
// and column to R and C array.
R.Add(r);
C.Add(c);
}
// z to count number of zero
int z = 0;
// Try block to check zero division error
try {
for (int i = 0; i < N; i++)
if (C[i] % R[i] == 0)
z = z + 1;
}
// If zero division error occur return -1
catch (Exception e) {
Console.WriteLine("Something went wrong" + e);
return -1;
}
// If z greater than equal to 2 return 1
if (z >= 2) {
return 1;
}
// Else return 0
else {
return 0;
}
}
// Driver Code
public static void Main(string[] args)
{
// # Matrix M
List > M
= new List >();
List temp1 = new List(){1, 2, 3};
List temp2 = new List(){4, 5, 6};
List temp3 = new List(){7, 8, 9};
M.Add(temp1);
M.Add(temp2);
M.Add(temp3);
// N length of matrix M
int N = M.Count;
// Call zerodiv function
int x = ZeroDiv(M, N);
// Print the answer
Console.WriteLine(x);
}
}
// This code is contributed by ukasp.
Javascript
输出
1
时间复杂度: O(N*N)
辅助空间: 在)