离散数学 之 序偶的关系(自反性,对称性,传递性)

  • 给定一个整数表示元素集合个数
  • 会随机生成num个序偶,打印出来。并自动判断关系
  • 入输入0,表示退出。
import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int i=scanner.nextInt();
        while (i!=0){
            new ZuoYe(i);
            i=scanner.nextInt();
        }
        System.out.println("退出");
    }
}


class ZuoYe {
    int Max;
    int[][] arr;
    int num;
    Random random;

    public ZuoYe(int max) {
        this.Max = max;
        arr = new int[max][max];
        this.random = new Random();
        this.num = this.random.nextInt(max * max) ;
        int row;
        int line;
        if (num==0){
            System.out.print("空");
        }else {
            for (int i = 0; i < this.num; i++) {
                row = random.nextInt(max);
                line = random.nextInt(max);
                if (arr[row][line] == 1) {
                    --i;
                } else {
                    arr[row][line] = 1;
                    if (i==this.num-1)
                        System.out.print(new Node(row+1,line+1).toString());
                    else
                        System.out.print(new Node(row+1,line+1).toString()+" ,");
                }
            }
        }
        System.out.println();

        isR();
        isS();
        isT();
    }

    public void isR() {
        for (int i = 0; i < this.Max; i++) {
            if (arr[i][i] == 0) {
                System.out.println("反自反性");
                return;
            }
        }
        System.out.println("自反性");
    }

    public void isS() {
        for (int i = 0; i < this.Max; i++) {
            for (int j = 0; j < this.Max; j++) {
                if (arr[i][j] == 1) {
                    if (arr[j][i] == 0) {
                        System.out.println("反对称性");
                        return;
                    }
                }
            }
        }
        System.out.println("对称性");
    }

    public void isT() {
        for (int a = 0; a < this.Max; a++) {
            for (int b = 0; b < this.Max; b++) {
                if (arr[a][b] == 1) {
                    for (int c = 0; c < this.Max; c++) {
                        if (arr[b][c] == 1) {
                            if (arr[a][c] == 0) {
                                System.out.println("没有传递性");
                                return;
                            }
                        }
                    }
                }
            }
        }
        System.out.println("具有传递性");
    }
    public static class Node{
        int i;
        int j;
        public Node(int i,int j){
            this.i=i;
            this.j=j;
        }

        @Override
        public String toString() {
            return "< " +
                    i +
                    ", " + j +
                    " >";
        }
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「 25' h 」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值