好像每个计算机专业的都要学人工智能,老师就会留这个作业,这是我的答案,穷举的,拿出来晒晒,帮忙大家写作业喽。
算法旋转数组那地方看似有问题,但是是没问题的,因为周期内数组会复原。朋友们拿去玩吧!
/**
 * @author 赵健 Nov 3, 2008 1:24:15 PM
 */
public class SumCircle {
public static void main(String[] args) {
// a其实是不用转的,固定它
int a[] = new int[] { 2, 5, 1, 5, 3, 4, 3, 2 };
int b[] = new int[] { 1, 3, 4, 5, 3, 5, 2, 2 };
int c[] = new int[] { 3, 2, 1, 3, 4, 2, 5, 1 };
int d[] = new int[] { 2, 3, 4, 1, 3, 4, 5, 3 }; int total = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < c.length; j++) {
for (int k = 0; k < d.length; k++) {
System.out.println("第" + i + j + k + "次(8进制)");
round(b, i);
round(c, j);
round(d, k);
print(a, b, c, d);
if (chk(a, b, c, d)) {
System.out.println("                      第"
+ (++total) + "次符合条件的结果=======>" + i + " , "
+ j + " , " + k);
}
}
}
}
} // 测试是否符合条件所有的都=12,调用oneRound()
public static boolean chk(int a[], int b[], int c[], int d[]) {
boolean chk = true;
for (int i = 0; i < a.length; i++) {
if (!oneRound(a[i], b[i], c[i], d[i])) {
chk = false;
break;
}
}
return chk;
} // 测试每次旋转是否符合条件=12
public static boolean oneRound(int aa, int bb, int cc, int dd) {
if ((aa + bb + cc + dd) == 12) {
return true;
} else {
return false;
}
} // 根据指定的圈数旋转圆盘
public static void round(int ar[], int count) {
for (int i = 0; i < count; i++) {
for (int j = 0; j < ar.length - 1; j++) {
ar[j] = ar[j] ^ ar[j + 1];
ar[j + 1] = ar[j] ^ ar[j + 1];
ar[j] = ar[j] ^ ar[j + 1];
}
}
} // 打印4个数组
public static void print(int a[], int b[], int c[], int d[]) {
int r[][] = new int[][] { a, b, c, d };
for (int i = 0; i < r.length; i++) {
for (int j = 0; j < r[i].length; j++) {
System.out.print(r[i][j] + " ");
}
System.out.println();
}
}
}

解决方案 »

  1.   

    package intellect;import java.util.*;
    /*
     * 操某人出品
     */
    public class AI {
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    AI M = new AI();//小子这句都不要? // 各个圈的数据分别如下 int num1[] = new int[] { 1, 5, 2, 2, 3, 4, 3, 5 };// 第一个小圈
    int num2[] = new int[] { 4, 3, 1, 2, 2, 5, 3, 5 };// 第二个小圈
    int num3[] = new int[] { 1, 2, 3, 1, 5, 2, 4, 3 };// 第三个小圈
    int num4[] = new int[] { 4, 3, 2, 3, 5, 4, 3, 1 };// 第四个小圈 List<int[]> list1 = new ArrayList<int[]>();
    list1.add(num1);
    List<int[]> list2 = new ArrayList<int[]>();
    M.species(list2, num2);//有八种
    List<int[]> list3 = new ArrayList<int[]>();
    M.species(list3, num3);//有八种
    List<int[]> list4 = new ArrayList<int[]>();
    M.species(list4, num4);//有八种
    for (int i = 0; i < list2.size(); i++) {
    for (int j = 0; j < list3.size(); j++) {
    for (int k = 0; k < list4.size(); k++) {
    boolean b = M.match(list1.get(0), list2.get(i), list3.get(j), list4.get(k));
    if (b) {
    System.out.println("结果:");
    M.p(list1.get(0));
    M.p(list2.get(i));
    M.p(list3.get(j));
    M.p(list4.get(k));
    }
    }
    }
    }
    } public static boolean match(int[] a, int[] b, int[] c, int[] d) {
    for (int i = 0; i < a.length; i++) {
    int num = a[i] + b[i] + c[i] + d[i];
    if (num == 12) continue;
    else return false;
    }
    return true;
    } public static void p(int[] a) {
    for (int i = 0; i < a.length; i++) {
    System.out.print(a[i]);
    }
    System.out.println();
    }
    public static void species(List<int[]> list , int[] arr){
    list.add(arr);
    for (int i = 0; i < arr.length - 1; i++) {
    int[] temp = new int[8];
    for (int j = i, k = 0; k < arr.length; j++, k++) {
    temp[k] = arr[(j + 1) % 8];
    }
    list.add(temp);
    }
    }
    }