每一题都有4个选项,总共有10题,因此应该有$4^{10}$种可能,枚举所有可能的情况
使用dfs暴力求解
1,2,3,4代表A,B,C,D
ArrayList<Integer> ans
存储每题的答案
ans.get(i)
代表取第i+1
的答案
import java.util.ArrayList;
import java.util.List;
public class test {
static ArrayList<Integer> ans = new ArrayList<>();
static List<ArrayList<Integer>> res = new ArrayList<>();
public static void main(String[] args) {
dfs(0);
}
public static void dfs(int count) {
if (count >= 10) {
if (!q1()) return; //去除不符合题目1的情况
if (!q2()) return;
if (!q3()) return;
if (!q4()) return;
if (!q5()) return;
if (!q6()) return;
if (!q7()) return;
if (!q8()) return;
if (!q9()) return;
for (int i : ans) System.out.print(i + " ");
System.out.println();
res.add(ans);
return;
}
for (int i = 1; i <= 4; i ++) {
ans.add(i);
dfs(count + 1);
ans.remove(ans.size() - 1);
}
}
public static boolean q1() { // 第一题
int firstA = -1; // 第一个A答案出现的位置
for (int i = 0; i < 5; i ++) {
if (ans.get(i) == 1) {
firstA = i;
break;
}
}
if (firstA + 1 == ans.get(0)) return true; // 符合四个选项可能情况返回true
else return false;
}
public static boolean q2() {
int count = 0;
int pos = -1;
for (int i = 1; i < 10; i ++) {
if (ans.get(i) == ans.get(i - 1)) {
count ++;
pos = i - 1;
}
}
if (count != 1) return false; // 必有一对连续两个相同答案
if (ans.get(1) + 3 == pos) return true;
else return false;
}
public static boolean q3() {
int q3ans = ans.get(2);
if (q3ans == 1 && ans.get(3) == 1) return true;
if (q3ans == 2 && ans.get(8) == 2) return true;
if (q3ans == 3 && ans.get(7) == 3) return true;
if (q3ans == 4 && ans.get(1) == 4) return true;
else return false;
}
public static boolean q4() {
int count = 0;
for (int i = 0; i < 10; i ++) {
if (ans.get(i) == 1) count ++ ;
}
int q4ans = ans.get(3);
if (q4ans + count != 6) return false;
// if (q4ans == 2 && count != 4) return false; // 上一行合并四个答案的枚举情况
// if (q4ans == 3 && count != 3) return false;
// if (q4ans == 4 && count != 2) return false;
return true;
}
public static boolean q5() {
int q5ans = ans.get(4);
if (q5ans != ans.get(q5ans - 1)) return false;
return true;
}
public static boolean q6() {
int countA = 0, countB = 0, countC = 0, countD = 0;
for (int i = 0; i < 10; i ++) {
int t = ans.get(i);
if (t == 1) countA++;
else if (t == 2) countB++;
else if (t == 3) countC++;
else if (t == 4) countD++;
}
int q6ans = ans.get(5);
if (countA == countC && q6ans == 2) return true;
if (countA == countC && q6ans == 3) return true;
if (countA == countD && q6ans == 4) return true;
if (countA != countB && countA != countC && countA != countD && q6ans == 1) return true;
return false;
}
public static boolean q7() {
int dif = Math.abs(ans.get(6) - ans.get(7));
int q7ans = ans.get(6);
if (-1 * q7ans + 4 != dif) return false;
return true;
}
public static boolean q8() {
int count = 0;
for (int i = 0; i < 10; i ++) {
if (ans.get(i) == 1) count ++ ;
}
int q8ans = ans.get(7);
if (q8ans - 1 != count) return false;
else return true;
}
public static boolean q9() {
int count = 0;
for (int i = 0; i < 10; i ++) {
if (ans.get(i) == 1) count ++ ;
}
count = 10 - count;
int q9ans = ans.get(8);
if (q9ans == 1 && (count == 4 || count == 6 || count == 8)) return true;
if (q9ans == 2 && (count == 3 || count == 5 || count == 7)) return true;
if (q9ans == 3 && count < 5) return true;
if (q9ans == 4 && (count == 1 || count == 4 || count == 9)) return true;
return false;
}
}
运行的最终答案为 acbcacddba