【等价关系、笛卡尔积、集合运算、等价类(JAVA)实现】形式语言与自动机理论。

总序:代码块中,第一部分为Main函数中代码,下半部分为相应的函数。
1.编写程序判断一个给定的关系R是否是等价关系
要求:
输入: 输入的关系中的每个元素均为自然数;
输出: R是否是等价关系
System.out.println("输入关系矩阵R的阶数n:");
		int n;
		n = sc.nextInt();
		System.out.println("关系矩阵的值:");
		int [][] R = new int[n][n];
		for(int i = 0; i < n; i ++) {
			for(int  j = 0; j < n; j ++) {
				R[i][j] = sc.nextInt();
			}
		}
		if(dengjia(R)) {
			System.out.println("R是等价关系");
		}else System.out.println("R不是等价关系");



public static boolean dengjia(int[][] r) {
		int zifan = 1;
		int duicheng = 1;
		int chuandi = 1;
		for(int i = 0; i < r.length; i ++) {
			if(r[i][i] != 1) {
				return false;
			}
		}
		for(int i = 0; i< r.length; i ++) {
			for(int j = 0; j < r.length; j ++) {
				if(r[i][j] != r[j][i]) {
					return false;
				}
			}
		}
		for(int i = 0; i < r.length; i ++) {
			for(int  j = i + 1; j < r.length; j ++) {
				for(int k = j + 1; k < r.length; k ++) {
					if(r[i][j] == 1 && r[j][k] == 1 && r[i][k] != 1) {
						return false;
					}
				}
			}
		}
		return true;
	}

运行结果展示:

2.编写函数,输出一个斐波那契数列
要求:
输入:一个自然数a;
输出:小于等于a的一个斐波那契数列
System.out.println("请输入a的值:");
		int a = sc.nextInt();
		LinkedList<Integer> li = new LinkedList<>();
		li = feibo(a);
		for(int i = 0; i < li.size(); i ++) {
			System.out.print(li.get(i) + " ");
		}
		System.out.println();



public static LinkedList<Integer> feibo(int a) {
		LinkedList<Integer>fbnq = new LinkedList<>();
		fbnq.add(0);
		fbnq.add(1);
		while(fbnq.get(fbnq.size() - 1) + fbnq.get(fbnq.size() - 2) < a) {
			fbnq.add(fbnq.get(fbnq.size() - 1) + fbnq.get(fbnq.size() - 2));
		}
		return fbnq;
	}

运行结果展示:

3.给定两个集合A和B,
的笛卡尔积。
输入: 集合A中的元素时字符,集合B中的元
素时自然数
要求:
输出:A和B的笛卡尔积
LinkedList<Integer> B = new LinkedList<>();
		String A;
		System.out.println("请输入A的值:");
		A = sc.nextLine();
		System.out.println("请输入B的值:(输入#结束)");
		while(!sc.hasNext("#")) {
			B.add(sc.nextInt());
		}
		LinkedList<LinkedList> dkj = new LinkedList<>();
		dkj = dikaer(A.toCharArray(), B);
		for (LinkedList<Integer> list : dkj) {
			System.out.print("(" + (char)(list.get(0) + 'A') + ", " + list.get(1) + ")" + " ");
		}
		System.out.println();

public static LinkedList<LinkedList> dikaer(char A[], LinkedList<Integer> B) {
		//A * B
		LinkedList<LinkedList> dkj = new LinkedList<>();
		for(int i = 0; i < A.length; i ++) {
			for(int j = 0; j < B.size(); j ++) {
				LinkedList<Integer> li = new LinkedList<>();
				li.add(A[i] - 'A');
				li.add(B.get(j));
				dkj.add(new LinkedList<>(li));
			}
		}
		return dkj;
	}

运行结果展示:

4.编写函数,实现集合的并、交、差和补运算
输入: 集合A和B,集合A和B中的元素均为自然数
输出: 集合A和B的并、交、差和补集合
LinkedList<Integer> B = new LinkedList<>();
		LinkedList<Integer> A = new LinkedList<>();
		System.out.println("请输入A的值:(输入#结束)");
		while (true) {  
            if (sc.hasNextInt()) {  
                A.add(sc.nextInt());  
            } else if (sc.hasNext("#")) {  
                sc.next(); 
                break; // 结束A的输入  
            }
        }  
		System.out.println("请输入B的值:(输入#结束)");
		while (true) {  
            if (sc.hasNextInt()) {  
                B.add(sc.nextInt());  
            } else if (sc.hasNext("#")) {  
                sc.next();  
                break; // 结束A的输入  
            }
        }  
		LinkedList<LinkedList> re = new LinkedList<>();
		re = yunsuan(A, B);
		int cnt = 0;
		for(LinkedList<Integer> list : re) {
			if(cnt == 0) System.out.print("并集:  ");
			if(cnt == 1) System.out.print("A 交 B:  ");
			if(cnt == 2) System.out.print("A - B:   ");
			if(cnt == 3) System.out.print("A - B 并 B - A:  ");
			for(int i = 0; i < list.size(); i ++) {
				System.out.print("(" + list.get(i) + ")" + " ");
			}
			System.out.println();
			cnt ++;
		}
		System.out.println();






public static LinkedList<LinkedList> yunsuan(LinkedList<Integer> A, LinkedList<Integer> B){
//		result[0]存放 A并B的结果,result[1]存放 A交B的结果,result[2]存放 A-B的结果,result[3]存放A - B并 B - A
		//并运算
		LinkedList<LinkedList> result = new LinkedList<>();
		Set<Integer> set = new LinkedHashSet<>();
		for(int i = 0; i < A.size(); i ++) {// 存放A
			set.add(A.get(i));
		}
		for(int i = 0; i < B.size(); i ++) {// 存放B
			set.add(B.get(i));
		}
		result.add(new LinkedList<>(set));
		//交运算-> A交B
		HashMap<Integer, Integer>map = new HashMap<>();
		HashMap<Integer, Integer>map2 = new HashMap<>();
		LinkedList<Integer> li = new LinkedList<>();
		for(int i = 0; i < A.size(); i ++) {
			if(!map.containsKey(A.get(i))) {//把A中的元素放入map中
				map.put(A.get(i), 1);
			}
		}
		for(int i = 0;i < B.size(); i ++) {
			if(!map2.containsKey(B.get(i))) {
				map2.put(B.get(i), 1);
			}
			if(map.containsKey(B.get(i))) {//遍历map中的元素,若map中含有,则将其删除。
				map.remove(B.get(i));
				li.add(B.get(i));
			}
		}//之后map中剩余的元素就是A - B的结果。
		result.add(new LinkedList<>(li));
		//差运算-> A - B = A - A交B;
		li.clear();
		for(Integer key: map.keySet()) {
			li.add(key);
		}
		result.add(new LinkedList<>(li));
		//补集合 A - B 并 B - A
		li.clear();
		for(int i = 0; i < result.get(result.size() - 2).size(); i ++) {
			int re = (int) result.get(result.size() - 2).get(i);
			if(!map2.containsKey(re)) {
				li.add(re);
			}
		}
		for(int i = 0; i < result.get(result.size() - 1).size(); i ++) {
			int re = (int) result.get(result.size() - 1).get(i);
			li.add(re);
		}
		result.add(new LinkedList<>(li));
		return result;
	}

运行结果展示:

5.给定一个任意的自然数集合,将该集合按模5同余
关系分成5个等价类。
int[] A = new int[30];
		Random r = new Random();
		for(int i = 0; i < A.length; i ++) {
			A[i] = r.nextInt(200) + 1;
		}
		LinkedList<LinkedList> re = new LinkedList<>();
		re = five(A);
		int cnt = 0;
		for(LinkedList<Integer> list : re) {
			System.out.print(cnt + "{");
			for(int i = 0; i < list.size(); i ++) {
				System.out.print(list.get(i) + ", ");
			}
			System.out.println("}");
			cnt ++;
		}
		System.out.println();





public static LinkedList<LinkedList> five(int[] A){
		LinkedList<LinkedList> result = new LinkedList<>();
		LinkedList<Integer> zero = new LinkedList<>();//模5余0,以此类推
		LinkedList<Integer> one = new LinkedList<>();
		LinkedList<Integer> two = new LinkedList<>();
		LinkedList<Integer> three = new LinkedList<>();
		LinkedList<Integer> four = new LinkedList<>();
		for(int i = 0; i < A.length; i ++) {
			if(A[i] % 5 == 0) {
				zero.add(A[i]);
			}
			if(A[i] % 5 == 1) {
				one.add(A[i]);
			}
			if(A[i] % 5 == 2) {
				two.add(A[i]);
			}
			if(A[i] % 5 == 3) {
				three.add(A[i]);
			}
			if(A[i] % 5 == 4) {
				four.add(A[i]);
			}
		}
		result.add(new LinkedList<>(zero));
		result.add(new LinkedList<>(one));
		result.add(new LinkedList<>(two));
		result.add(new LinkedList<>(three));
		result.add(new LinkedList<>(four));
		return result;
	}

运行结果展示:

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值