주어진 정방형 행렬에서 행과 열이 같고 길이가 홀수인 mat[row][column]은 행과 열의 수가 홀수여야 함을 의미합니다. 즉, 다음으로 나눌 수 없습니다. 2, 작업은 해당 행렬의 중간 행과 중간 열의 곱을 찾는 것입니다.
아래 주어진 그림과 같이 -

제약조건
-
행렬은 정방 행렬이어야 합니다.
-
열과 행의 길이가 홀수여야 합니다.
입력
mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}} 출력
Product of middle row = 120 Product of middle column = 80
설명
Product of middle row = 4 * 5 * 6 = 120 Product of middle column = 2 * 5 * 8 = 80
입력
mat[][] = {{3, 5, 0},
{1, 2, 7},
{9, 0, 5}} 출력
Product of middle row = 14 Product of middle column = 0
설명
Product of middle row = 1 * 2 * 7 = 120 Product of middle column = 5 * 2 * 0 = 0
문제를 해결하기 위해 다음과 같은 접근 방식을 사용합니다.
-
행렬 mat[][]를 입력으로 사용합니다.
-
중간 행과 중간 열에서와 같이 행렬을 순회합니다.
-
가운데 행과 가운데 열의 곱을 계산하여 결과를 반환합니다.
알고리즘
Start
In function int product(int mat[][MAX], int n)
Step 1→ Declare and initialize rproduct = 1, cproduct = 1
Step 2→ Loop For i = 0 and i < n and i++
Set rproduct = rproduct * mat[n / 2][i]
Set cproduct = cproduct * mat[i][n / 2]
Step 3→ Print "Product of middle row: rproduct “
Step 4→ Print "Product of middle column: cproduct”
In function int main()
Step 1→ Declare and initialize mat[][MAX] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } }
Step 2→ Call product(mat, MAX)
Stop 예시
#include <stdio.h>
#define MAX 3
int product(int mat[][MAX], int n){
int rproduct = 1, cproduct = 1;
//We will only check the middle elements and
//find their products
for (int i = 0; i < n; i++) {
rproduct *= mat[n / 2][i];
cproduct *= mat[i][n / 2];
}
// Printing the result
printf("Product of middle row: %d\n", rproduct);
printf("Product of middle column: %d\n", cproduct);
return 0;
}
// Driver code
int main(){
int mat[][MAX] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
product(mat, MAX);
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Product of middle row: 120 Product of middle column: 80