首页 > 编程知识 正文

离散数学中的传递关系,离散数学传递关系详解

时间:2023-05-05 13:06:07 阅读:272633 作者:2573

给定一个整数表示元素集合个数会随机生成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五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。