离散数学---Java编程实现关系性质的判断

  1. 理解二元关系的矩阵表示
  2. 理解二元关系的自反性、反自反性、对称性、反对称性及传递性

用矩阵表示二元关系

通过矩阵的特征判断二元关系所具有的性质

运用二维数组实现矩阵的输入,然后判断自反性,反自反性,对称性,反对称性,传递性

import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
            System.out.print("请输入矩阵阶数:");
            int n = sca.nextInt();
            Matrix m = new Matrix(n);
            System.out.println("请输入关系矩阵R(0或1):");
            int[][] array = m.getArray();
            //矩阵的输入
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    int input = sca.nextInt();
                    if (input != 0 && input != 1) {
                        System.out.println("输入数据错误,请重新输入:");
                    } else {
                        array[i][j] = input;
                    }
                }
            }
            m.matrixPrint();
            Reflexivity ref = new Reflexivity();
            ref.array = m.array;
            ref.judge();
            Symmetric sym = new Symmetric();
            sym.array = m.array;
            sym.judge();
            Transitivity tran = new Transitivity();
            tran.array = m.array;
            tran.judge();
    }
}
class Matrix{
    int n;//矩阵的阶数
    int[][] array;//二元数组表示矩阵
    public Matrix(int n){
        this.n=n;
        this.array=new int[n][n];
    }
    public int[][] getArray(){
        return array;
    }

    public void matrixPrint(){//矩阵的输出
        System.out.println("输出矩阵R:");
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array.length;j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}
//自反性判断
class Reflexivity{
    boolean reflexivity=true;
    boolean noReflexivity=true;
    int[][] array;

    //自反性
    public boolean isRef(){
        for(int i=0;i<array.length;i++){
           if(array[i][i]!=1) {
              reflexivity=false;
              break;
           }
        }
        return reflexivity;
    }
    //反自反性
    public boolean noIsRef(){
        for(int i=0;i<array.length;i++){
            if(array[i][i]!=0) {
              noReflexivity=false;
              break;
            }
        }
        return noReflexivity;
    }
    public void judge(){
        System.out.print("自反性判断:");
       if(this.isRef()){
           System.out.println("具有自反性。");
       }else if(this.noIsRef()){
           System.out.println("具有反自反性。");
        }else{
           System.out.println("既不是自反的,也不是反自反的。");
       }
    }
}
//对称性判断
class Symmetric{
    int[][] array;
    boolean symmetric=true;
    boolean noSymmetric=true;
    public boolean isSym(){
        for(int i=0;i< array.length;i++){
            for(int j=0;j< array.length;j++){
                if(array[i][j]!=array[j][i]){
                    symmetric=false;
                    break;
                }
            }
        }
        return symmetric;
    }
    public boolean noIsSym(){
        for(int i=0;i< array.length;i++){
            for(int j=0;j< array.length;j++){
                if(i!=j){
                    if(array[i][j]==1&&array[j][i]==1){
                        noSymmetric=false;
                        break;
                    }
                }
            }
        }
        return noSymmetric;
    }
    public void judge(){
        System.out.print("对称性判断:");
        if(this.isSym()){
            System.out.println("具有对称性。");
        }else if(this.noIsSym()){
            System.out.println("具有反对称性。");
        } else{
            System.out.println("既不是对称的,也不是反对称的。");
        }
    }
}
//传递性判断
class Transitivity{
    int[][] array;
    boolean tran=true;
    public void judge(){
        System.out.print("传递性判断:");
        for(int i=0;i<array.length;i++){
            for(int j=0;j< array.length;j++){
                for(int k=0;k< array.length;k++){
                    if(array[i][j]==1&&array[j][k]==1){
                        if(array[i][k]!=1){
                           tran=false;
                        }
                    }
                }
            }
        }
        if(tran){
            System.out.println("具有对称性");
        }else{
            System.out.println("不具有对称性");
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

用户1234567890

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值