zoukankan      html  css  js  c++  java
  • 【离散数学】实验二 集合上二元关系性质判定的实现

    先打个草稿,今晚看书理解下,明天实现。

    内容:

    编程实现任意集合上二元关系的性质判定。

    要求:

    能正确判定任意二元关系的自反性、对称性、传递性、反自反性和反对称性。

    代码:

    /*
     * 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

    */

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    设计模式 -- 中介者设计模式 (Mediator Pattern)
    java.lang.IllegalArgumentException: View not attached to window manager
    项目中处理android 6.0权限管理问题
    Python File.readlines() 方法
    notepad++快捷键
    ora-00054:resource busy and acquire with NOWAIT specified
    空格和TAB键混用错误:IndentationError: unindent does not match any outer indentation level
    Notepad++编辑Pyhton文件的自动缩进的问题(图文)
    echoawksed eecurl的使用-shell
    python正则表达式
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965348.html
Copyright © 2011-2022 走看看