您的位置:首页 > 大数据 > 人工智能

人工智能作业--如何转动使得每个扇区数字和为12

2008-11-11 10:22 274 查看
这是我的实现代码:

/**
* @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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐