【离散数学】实验二 集合上二元关系性质判定的实现
来源:互联网 发布:水冷 风冷 知乎 编辑:程序博客网 时间:2024/05/17 03:28
先打个草稿,今晚看书理解下,明天实现。
内容:
编程实现任意集合上二元关系的性质判定。
要求:
能正确判定任意二元关系的自反性、对称性、传递性、反自反性和反对称性。
代码:/* * Author : Tob_yuhong * Function: 集合上二元关系性质判定的实现,能正确判定任意二元关系的自反性、对称性、传递性、反自反性和反对称性。 * 编译环境: Code::Blocks 13.12 *///分别将自反性、对称性、传递性、反自反性和反对称性编号为Func1、Func2,...,一直到Func5。#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <cstring>#include <vector>#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();void Func4();void Func5();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(relation, 0, sizeof(relation)); int 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(); //判断是否满足反自反性 Func4(); //判断是否满足反对称性 Func5(); 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++) { for(int j = 2; j <= num; j++) { if(arr[i][2] == arr[j][1]) { 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; }}void Func4(){ bool flag = true; for(int i = 1; i <= nnn; i++) { if(relation[i][i] != 0) { flag = false; break; } } if(flag == true) { cout << "满足反自反性" << endl; } else { cout << "不满足反自反性" << endl; }}void Func5(){ bool flag = true; for(int i = 1; i <= nnn - 1; i++) { for(int j = i + 1; j <= nnn; j++) { if(relation[i][j] == 1 && relation[j][i] == 1 && i != j) { flag = false; break; } } } if(flag == true) { cout << "满足反对称性" << endl; } else { cout << "不满足反对称性" << endl; }}运行结果示意:
测试样例:
/*
4
8
1 1
1 3
2 2
3 3
3 1
3 4
4 3
4 4
*/
1 0
- 【离散数学】实验二 集合上二元关系性质判定的实现
- 【离散数学】【改进版】实验二 集合上二元关系性质判定的实现
- 南邮离散数学实验2 集合上二元关系性质判定的实现
- 集合上二元关系性质的判定
- 离散数学代码实现 二元关系的可传递性判断
- 【离散数学实验】关系R的性质判断
- 离散数学-二元关系、闭包的概念
- 判定二元关系的可传递性
- 离散数学之集合上关系的判断
- HashSet 集合性质实验
- 【离散数学】实验三 偏序关系中盖住关系的求取及格论中有补格的判定
- 南京邮电大学离散数学实验三偏序关系中盖住关系的求取及格论中有补格的判定
- <<离散数学上机实验>>集合上机实验
- 南邮离散数学实验二
- 判断集合二元关系的符合运算
- 离散数学 集合的运算
- 南邮离散数学实验3 偏序关系中盖住关系的求取及格论中有补格的判定
- 南邮离散数学实验三-偏序关系中盖住关系的求取及格论中有补格的判定
- 我才不是萝莉控呢
- Android 自定义View流程
- xpath学习(一)
- 使用Putty连接Ubuntu虚拟机
- Android Service使用方法--简单音乐播放实例
- 【离散数学】实验二 集合上二元关系性质判定的实现
- matlab读取/播放视频的函数(2)
- hdu 5311 Hidden String dfs
- 树懒_移动开学笔记_day29&day30_代码管理与实战
- 9.4 Noip模拟
- Codeforces Gym 100325A String Decomposition 字符串DP
- 第七章定制并发类
- 显示桌面
- C语言中sprintf()函数的用法