이진 행렬은 모든 요소가 이진 값, 즉 0 또는 1인 행렬입니다. 이진 행렬은 부울 행렬, 관계형 행렬, 논리 행렬이라고도 합니다. .
아래의 예
$$\begin{bmatrix} 0 &1 &0
\\ 1 &1 &0
\\ 1 &0 &1
\\ \end {bmatrix}\:\:\:\:\:\:\:\:\:
\begin{bmatrix}
0 &3 &0
\\ 1 &1 &0
\\ 1 &0 &2
\\ \end{bmatrix}\\\tiny This\:is\:a\:Binary\:Matrix\:\:\:\:\:\:\:
이것은\:is\:not\:a\:binary\:matrix$$
위 그림에서 왼쪽의 첫 번째 행렬은 이진 행렬이고 다른 행렬에는 이진(0 또는 1)이 아닌 일부 값이 빨간색으로 강조 표시됩니다. 즉, 3과 2는 이진 행렬이 아닙니다.
예시
Input: m[4][3] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 0, 0 } } Output: its a binary matrix
접근
우리는 전체 행렬을 순회할 수 있고 0 또는 1이면 모든 요소를 검사하고 그것을 이진 행렬로 인쇄하고 그렇지 않으면 이진 행렬이 아닌 것으로 인쇄합니다.
알고리즘
Start Step 1 -> define macros as #define row 3 and #define col 4 Step 2 -> Declare function to check if a matrix is binary matrix or not bool check(int arr[][col]) Loop For int i = 0 and i < row and i++ Loop For int j = 0 and j < col and j++ IF(!(arr[i][j] = 0 || arr[i][j] = 1)) return false End End End return true step 3 -> In main() Declare an array as int arr[row][col] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 0, 0 } } If (check(arr)) Print its a binary matrix Else Print its not a binary matrix Stop
예시
#include <bits/stdc++.h> using namespace std; #define row 3 #define col 4 //check if a matrix is binary matrix or not bool check(int arr[][col]){ for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (!(arr[i][j] == 0 || arr[i][j] == 1)) return false; } } return true; } int main(){ int arr[row][col] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 0, 0 } }; if (check(arr)) cout << "its a binary matrix"; else cout << "its not a binary matrix"; return 0; }
출력
its a binary matrix