행 개수가 'r'이고 열 개수가 'c'인 두 행렬 M1[r][c]와 M2[r][c]가 주어졌을 때 두 행렬이 동일한지 확인해야 합니다. 그들이 동일하면 "매트릭스가 동일합니다"를 인쇄하고 그렇지 않으면 "매트릭스가 동일하지 않습니다"를 인쇄하십시오
동일한 매트릭스
두 행렬 M1과 M2는 −
일 때 동일하다고 합니다.- 두 행렬의 행과 열 수가 동일합니다.
- M1[i][j]의 값은 M2[i][j]와 같습니다.
아래 주어진 그림과 같이 3x3의 두 행렬 m1과 m2는 동일합니다 -
$$M1[3][3]=\begin{bmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end {bmatrix} \:\:\:\:M2 [3][3] =\begin{bmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{bmatrix} $$
예시
Input: a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3,3, 3, 3},
{3,3, 3, 3}};
b[n][n]= { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
Output: matrices are identical
Input: a[n][n] = { {2, 2, 2, 2},
{2, 2, 1, 2},
{3,3, 3, 3},
{3,3, 3, 3}};
b[n][n]= { {2, 2, 2, 2},
{2, 2, 5, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
Output: matrices are not identical 접근
두 행렬 a[i][j]와 b[i][j]를 반복하고, 모두 true이면 a[i][j]==b[i][j]를 확인한 다음 동일한지 인쇄하고 그렇지 않으면 인쇄합니다. 동일하지 않습니다.
알고리즘
Start
Step 1 -> define macro as #define n 4
Step 2 -> Declare function to check matrix is same or not
int check(int a[][n], int b[][n])
declare int i, j
Loop For i = 0 and i < n and i++
Loop For j = 0 and j < n and j++
IF (a[i][j] != b[i][j])
return 0
End
End
End
return 1
Step 3 -> In main()
Declare variable asint a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}}
Declare another variable as int b[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}}
IF (check(a, b))
Print matrices are identical
Else
Print matrices are not identical
Stop 예시
#include <bits/stdc++.h>
#define n 4
using namespace std;
// check matrix is same or not
int check(int a[][n], int b[][n]){
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i][j] != b[i][j])
return 0;
return 1;
}
int main(){
int a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
int b[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
if (check(a, b))
cout << "matrices are identical";
else
cout << "matrices are not identical";
return 0;
} 출력
matrices are identical