实验题目:编程实现关系性质的判断
1、自反性:主对角线元素全为1
2、反自反性:主对角线元素全为0
3、对称性:矩阵为对称矩阵
4、反对称性:如果a[i][j] = 1,且i != j,则a[j][i] = 0
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[4][4];
bool reflexivity = true; //自反性标记
bool disreflexivity = true; //反自反性标记
bool symmetry = true; //对称性标记
bool antisymmetry = true; //反对称性标记
//输入关系矩阵
cout << "请输入数组元素:" << endl;
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
cin >> a[i][j];
}
}
//判断自反性
//主对角线元素全为1,满足自反性
for (int i = 0; i < 4; ++i){
if (a[i][i] != 1){
reflexivity = false;
}
}
//判断反自反性
//主对角线元素全为0,满足反自反性
for (int i = 0; i < 4; ++i){
if (a[i][i] != 0){
disreflexivity = false;
}
}
//判断对称性
//矩阵为对称矩阵,满足对称性
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
if (a[i][j] != a[j][i]){
symmetry = false;
}
}
}
//判断反对称性
//如果a[i][j] = 1,且 i != j,则a[j][i] = 0
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
if (a[i][j] == 1 && i != j && a[j][i] != 0){
antisymmetry = false;
}
}
}
if (reflexivity == true){
cout << "矩阵满足自反性" << endl;
}
if (disreflexivity == true){
cout << "矩阵满足反自反性" << endl;
}
if (symmetry == true){
cout << "矩阵满足对称性" << endl;
}
if (antisymmetry == true){
cout << "矩阵满足反对称性" << endl;
}
return 0;
}