เมทริกซ์ไบนารีคือเมทริกซ์ที่มีองค์ประกอบทั้งหมดเป็นค่าไบนารีเช่น 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