当前位置: 代码迷 >> 综合 >> 离散数学-自反性-对称性-传递性,关系的性质
  详细解决方案

离散数学-自反性-对称性-传递性,关系的性质

热度:97   发布时间:2023-11-25 10:29:38.0
暂时当草稿存放在这里,后面再补充

//分别将自反性、对称性、传递性的编号为Func1、Func2,Func3。
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;const int LEN = 140 + 10;
int arr[LEN][2+10];      //存储集合元素
int relation[LEN][LEN];  //关系矩阵
int nnn; //集合元素个数
int num; //集合关系个数
void Func1();
void Func2();
void Func3();int main()
{//   freopen("datain.txt", "r", stdin);cout << "请输入集合中的元素个数 : " << endl;cin >> nnn;cout << "请输入集合中的关系个数 : " << endl;cin >> num;cout << "集合中一共有" << num << "对关系" << "," << num*2 <<"个元素(请以整数形式输入) : " << endl;memset(arr, 0, sizeof(arr));   //用数组做参数传递给标准函数memset(),以让其将数组设置成全0:memset(relation, 0, sizeof(relation)); //这里不懂得话可以参考https://blog.csdn.net/qq_37796444/article/details/80181632int num1, num2;for(int i = 1; i <= num; i++){cin >> num1 >> num2;arr[i][1] = num1;arr[i][2] = num2;relation[num1][num2] = 1;}cout << "输出关系矩阵 : " << endl;for(int i = 1; i <= nnn; i++){for(int j = 1; j <= nnn; j++){cout << relation[i][j] << "     ";}cout << endl;}cout << endl;cout << "判断结论 : " << endl;//判断是否满足自反性Func1();//判断是否满足对称性Func2();//判断是否满足传递性Func3();return 0;
}void Func1()   //判断是否满足自反性
{bool flag = true;for(int i = 1; i <= nnn; i++){if(relation[i][i] != 1){flag = false;break;}}if(flag == true){cout << "满足自反性" << endl;}else{cout << "不满足自反性" << endl;}
}void Func2()  //判断是否满足对称性
{bool flag = true;for(int i = 1; i <= nnn; i++){for(int j = 1; j <=nnn; j++){if(relation[i][j] != relation[j][i]){flag = false;}}}if(flag == true){cout << "满足对称性" << endl;}else{cout << "不满足对称性" << endl;}
}void Func3()  //判断是否满足传递性
{bool flag = true;for(int i = 1; i <= num - 1; i++)  //num表示关系个数{for(int j = 2; j <= num; j++){if(arr[i][2] == arr[j][1])   //arr数组表示存储集合元素{int num1 = arr[i][1], num2 = arr[j][2];if(relation[num1][num2] != 1){flag = false;break;}}}if(flag == false)break;}if(flag == true){cout << "满足传递性" << endl;}else{cout << "不满足传递性" << endl;}
}

  相关解决方案