📜  奇数方阵中间行和列的乘积

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

奇数方阵中间行和列的乘积

给定一个奇数维度的整数方阵 (3 * 3, 5 * 5)。任务是找到中间行和中间列元素的乘积。

例子:

Input: mat[][] = 
{{2, 1, 7},
 {3, 7, 2},
 {5, 4, 9}}
Output: Product of middle row = 42
        Product of middle column = 28
Explanation: Product of Middle row elements (3*7*2)
Product of Middle Column elements (1*7*4)

Input: mat[][] =
{ {1, 3, 5, 6, 7},
  {3, 5, 3, 2, 1},
  {1, 2, 3, 4, 5},
  {7, 9, 2, 1, 6},
  {9, 1, 5, 3, 2}}
Output: Product of middle row = 120
        Product of middle column = 450 

方法:由于给定矩阵的维数为奇数,因此中间的行和列将始终位于第 n/2 位。因此,从 i = 0 到 N 运行一个循环并生成中间行的所有元素,即row_prod *= mat[n / 2][i] 。同样,中间列元素的乘积将是col_prod *= mat[i][n / 2]

下面是上述方法的实现:

C++
// C++ program to find product of
// middle row and middle column in matrix
#include 
using namespace std;
const int MAX = 100;
 
void middleProduct(int mat[][MAX], int n)
{
 
    // loop for product of row and column
    int row_prod = 1, col_prod = 1;
    for (int i = 0; i < n; i++) {
        row_prod *= mat[n / 2][i];
        col_prod *= mat[i][n / 2];
    }
 
    // Print result
    cout << "Product of middle row = "
         << row_prod << endl;
 
    cout << "Product of middle column = "
         << col_prod;
}
 
// Driver code
int main()
{
    int mat[][MAX] = { { 2, 1, 7 },
                       { 3, 7, 2 },
                       { 5, 4, 9 } };
 
    middleProduct(mat, 3);
 
    return 0;
}


Java
// Java program to find product of
// middle row and middle column in matrix
import java.io.*;
 
class GFG {
 
 
static int MAX = 100;
 
static void middleProduct(int mat[][], int n)
{
 
    // loop for product of row and column
    int row_prod = 1, col_prod = 1;
    for (int i = 0; i < n; i++) {
        row_prod *= mat[n / 2][i];
        col_prod *= mat[i][n / 2];
    }
 
    // Print result
    System.out.print("Product of middle row = "
        + row_prod);
 
    System.out.print( "Product of middle column = "
        + col_prod);
}
 
    // Driver code
    public static void main (String[] args) {
            int mat[][] = { { 2, 1, 7 },
                    { 3, 7, 2 },
                    { 5, 4, 9 } };
 
    middleProduct(mat, 3);
    }
}
// This code is contributed by shs


Python3
# Python3 program to find product of
# middle row and middle column in matrix
 
MAX = 100
 
def middleProduct(mat, n):
 
    # loop for product of row and column
    row_prod = 1
    col_prod = 1
    for i in range(n) :
        row_prod *= mat[n // 2][i]
        col_prod *= mat[i][n // 2]
 
    # Print result
    print ("Product of middle row = ",
                             row_prod)
 
    print ("Product of middle column = ",
                                col_prod)
                                 
# Driver code
if __name__ == "__main__":
 
    mat = [[ 2, 1, 7 ],
           [ 3, 7, 2 ],
           [ 5, 4, 9 ]]
 
    middleProduct(mat, 3)
 
# This code is contributed by ita_c


C#
// C# program to find product of
// middle row and middle column in matrix
using System;
 
class GFG {
 
 
//static int MAX = 100;
 
static void middleProduct(int [,]mat, int n)
{
 
    // loop for product of row and column
    int row_prod = 1, col_prod = 1;
    for (int i = 0; i < n; i++) {
        row_prod *= mat[n / 2,i];
        col_prod *= mat[i,n / 2];
    }
 
    // Print result
    Console.WriteLine("Product of middle row = "
        + row_prod);
 
    Console.WriteLine( "Product of middle column = "
        + col_prod);
}
 
    // Driver code
    public static void Main () {
            int [,]mat = { { 2, 1, 7 },
                    { 3, 7, 2 },
                    { 5, 4, 9 } };
 
    middleProduct(mat, 3);
    }
}
// This code is contributed by shs


PHP


Javascript


输出:
Product of middle row = 42
Product of middle column = 28

时间复杂度: O(n)