最近接触了一门名为《自动机理论》的课程,主要讲述了计算机的数学模型(图灵模型)。现在终于知道了为什么都说计算机和数学联系很大了,其实现在我们使用的计算机都只是一个数学模型的物理呈现。
在课程中有一道关于如何将离散数学中闭包问题进行建模的问题,上网查找资料发现解决类似问题的作者都是使用的C完成的。难道咱强大的Java就做不到吗?很显然是可以的,经过自己的苦思总算解出了此问题。忽然发现平时多一些建模,有意识地将现实问题进行代码的抽象可以有助于完善我们的思维逻辑体系,现将代码贴于下方,希望可以帮助到遇到类似问题的朋友,也希望可以得到大家的优化。
package cn.Lisan;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Test {
//存储第一分量
private static String a;
//存储第二分量
private static String b;
//用量判断增加括号的计数器
private static int count = 0;
//存储关系R,最终用来存放自反闭包
private static Set<String> list = new HashSet<String>();
//存放关系R,最终用来存放对称闭包
private static Set<String> listR = new HashSet<String>();
//存放关系R,最终用来存放传递闭包
private static List<String> ChuanR = new ArrayList<String>();
//存储分量自身关系
private static Set<String> RR = new HashSet<String>();
//声明键盘输入
private static Scanner sc = new Scanner(System.in);
//主方法
public static void main(String[] args) {
setR();
deskport();
}
// 计算自反闭包
public static void Zifan() {
if (list.containsAll(RR)) {
System.out.println("{");
for (String zifan : list) {
count++;
if (count < list.size() - 1) {
System.out.print(zifan + ",");
} else {
System.out.print(zifan);
}
}
System.out.println("}");
} else {
list.addAll(RR);
System.out.print("{");
for (String zifan : list) {
count++;
if (count < list.size() - 1) {
System.out.print(zifan + ",");
} else {
System.out.print(zifan);
}
}
System.out.print("}");
}
System.out.println();
deskport();
}
// 计算对称闭包
public static void DuiChenfan() {
String a;
String b;
String str;
String[] st = listR.toArray(new String[0]);
for (int i = 0; i < st.length; i++) {
if (i < st.length) {
a = st[i].substring(3, 4);
b = st[i].substring(1, 2);
str = "(" + a + "," + b + ")";
listR.add(str);
}
}
System.out.print("{");
for (String zifan : listR) {
count++;
if (count < listR.size() - 1) {
System.out.print(zifan + ",");
} else {
System.out.print(zifan);
}
}
System.out.println("}");
deskport();
}
// 计算传递闭包
public static void ChuanDifan() {
String a;
String b;
String str;
String[] st = ChuanR.toArray(new String[0]);
for (int i = 0; i < st.length; i++) {
if (i < st.length - 1) {
if (st[i].substring(3, 4).equals(st[i + 1].substring(1, 2))) {
a = st[i].substring(1, 2);
b = st[i + 1].substring(3, 4);
str = "(" + a + "," + b + ")";
if (!ChuanR.contains(str)) {
ChuanR.add(str);
}
}
}
}
System.out.print("{");
for (String zifan : ChuanR) {
count++;
if (count < ChuanR.size() - 1) {
System.out.print(zifan + ",");
} else {
System.out.print(zifan);
}
}
System.out.println("}");
deskport();
}
// 生成关系R
public static void setR() {
System.out.println("请输入一组关系:(输入#结束)");
while (true) {
a = sc.nextLine();
if (!a.equals("#")) {
System.out.println("->");
b = sc.nextLine();
if (!RR.contains("(" + a + "," + a + ")")) {
RR.add("(" + a + "," + a + ")");
}
if (!RR.contains("(" + b + "," + b + ")")) {
RR.add("(" + b + "," + b + ")");
}
if (!list.contains("(" + a + "," + b + ")")) {
list.add("(" + a + "," + b + ")");
listR.add("(" + a + "," + b + ")");
ChuanR.add("(" + a + "," + b + ")");
System.out.println("(" + a + "," + b + ")");
} else {
continue;
}
} else {
break;
}
}
// for(String st:list){
// System.out.println(st);
// }
}
// 界面生成
public static void deskport() {
System.out.println("=================================");
System.out.println("输出自反闭包选择:1");
System.out.println("输出对称闭包选择:2");
System.out.println("输出传递闭包选择:3");
System.out.println("重新设置一组关系:4");
System.out.println("=================================");
int select = sc.nextInt();
switch (select) {
case 1:
Zifan();
break;
case 2:
DuiChenfan();
break;
case 3:
ChuanDifan();
break;
case 4:
setR();
break;
default:
break;
}
}
}
热门评论
赞!!!!!!!!!!!!!!!!!!!!!!!!