将每个 Matrix 元素替换为其行产品,但该元素除外
给定一个M*N阶的二维数组mat[][] 。任务是用同一行其他元素的乘积替换每一行的每个元素。
例子:
Input: mat[][] = {{3, 4, 1}, {6, 1, 7}, {2, 9, 8}}
Output:
4, 3, 12
7, 42, 6
72, 16, 18
Input: mat[][] = {{13, 4, 5}, {6, 11, 1}}
Output:
20, 65, 52
11, 6, 66
方法:这个想法很简单。逐行遍历矩阵并找到每一行的乘积。在第二次遍历中,找到行中每个单元格的替换值。请按照以下步骤解决问题:
- 初始化变量mul、i和j。
- 使用变量i遍历范围[0, M)并执行以下任务:
- 将mul的值设置为1。
- 使用变量j迭代范围[0, N)并执行以下任务:
- 将 mul 的值设置为mul *mat[i][j]。
- 使用变量j迭代范围[0, N)并执行以下任务:
- 将mat[i][j]的值设置为mul/mat[i][j]。
- 执行上述步骤后,打印mat[][]的值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
const int M = 3, N = 3;
// Show Matrix.
void showMatrix(vector >& mat)
{
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
// Utility Function
void ReplaceWithProduct(vector >& mat)
{
int mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i][j];
for (j = 0; j < N; j++)
mat[i][j] = mul / mat[i][j];
}
showMatrix(mat);
}
// Utility Main function.
int main()
{
vector > mat = { { 3, 6, 2 },
{ 1, 4, 9 },
{ 5, 3, 8 } };
ReplaceWithProduct(mat);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
static int M = 3, N = 3;
// Show Matrix.
static void showMatrix(int [][]mat)
{
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// Utility Function
static void ReplaceWithProduct(int [][]mat)
{
int mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i][j];
for (j = 0; j < N; j++)
mat[i][j] = mul / mat[i][j];
}
showMatrix(mat);
}
// Utility Main function.
public static void main(String args[])
{
int [][]mat = { { 3, 6, 2 },
{ 1, 4, 9 },
{ 5, 3, 8 } };
ReplaceWithProduct(mat);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 program for the above approach
M = 3
N = 3
# Show Matrix.
def showMatrix(mat):
for i in range(0, M):
for j in range(0, N):
print(mat[i][j], end=" ")
print()
# Utility Function
def ReplaceWithProduct(mat):
for i in range(0, M):
mul = 1
for j in range(0, N):
mul *= mat[i][j]
for j in range(0, N):
mat[i][j] = mul // mat[i][j]
showMatrix(mat)
# Utility Main function.
if __name__ == "__main__":
mat = [[3, 6, 2],
[1, 4, 9],
[5, 3, 8]]
ReplaceWithProduct(mat)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG
{
static int M = 3, N = 3;
// Show Matrix.
static void showMatrix(int [,]mat)
{
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
Console.Write(mat[i,j] + " ");
}
Console.WriteLine();
}
}
// Utility Function
static void ReplaceWithProduct(int [,]mat)
{
int mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i,j];
for (j = 0; j < N; j++)
mat[i,j] = mul / mat[i,j];
}
showMatrix(mat);
}
// Utility Main function.
public static void Main(String []args)
{
int [,]mat = { { 3, 6, 2 },
{ 1, 4, 9 },
{ 5, 3, 8 } };
ReplaceWithProduct(mat);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
12 6 18
36 9 4
24 40 15
时间复杂度: O(N*N)
辅助空间: O(1)