在知乎上看到一份推理题,如图
尝试通过编程来实现解答,首先想到思路是通过对每一道题的答案判断走不同的分支。但走到最后发现各个题目之间无法关联起来,而且工作量太大,最后选择用最直接最暴力的迭代来实现。
private String anwos[] = {"A","B","C","D"};//选项
private String staticA = "A";//答案选A
private String staticB = "B";//答案选B
private String staticC = "C";//答案选C
private String staticD = "D";//答案选D
String one;//第一题
String two;//第二题
String three;//第三题
String four;//第四题
String five;//第五题
String six;//第六题
String seven;//第七题
String eight;//第八题
String nine;//第九题
String ten;//十题
String countMaxAn;//出现次数最多的答案
String countMinAn;//出现次数最少的答案
int countMaxNumber;//出现次数最多
int countMinNumber;//出现次数最少
int countA = 0;//A出现的次数
int countB = 0;//B出现的次数
int countC = 0;//C出现的次数
int countD = 0;//D出现的次数
int count =0;//最终正确答案个数
/**
* 暴力
*/
@Test
public void test2() {
for(String ones : anwos) {
one = ones;
for(String twos : anwos) {
two = twos;
for(String threes : anwos) {
three = threes;
for(String fours : anwos) {
four = fours;
for(String fives : anwos) {
five = fives;
for(String sixs : anwos) {
six = sixs;
for(String sevens : anwos) {
seven = sevens;
for(String eights : anwos) {
eight = eights;
for(String nines : anwos) {
nine = nines;
for(String tens : anwos) {
ten = tens;
countTst2Number();//统计答案
boolean b = checks();//各题目校验
if(b) {
count++;
System.out.println(count);
System.out.println("one="+one);
System.out.println("two="+two);
System.out.println("three="+three);
System.out.println("four="+four);
System.out.println("five="+five);
System.out.println("six="+six);
System.out.println("seven="+seven);
System.out.println("eight="+eight);
System.out.println("nine="+nine);
System.out.println("ten="+ten);
System.out.println("countMaxAn="+countMaxAn);
System.out.println("countMinAn="+countMinAn);
System.out.println("----------------------------------------------");
}
}
}
}
}
}
}
}
}
}
}
}
// 统计最多/最少答案和次数
private void countTst2Number() {
defautTest(one);
defautTest(two);
defautTest(three);
defautTest(four);
defautTest(five);
defautTest(six);
defautTest(seven);
defautTest(eight);
defautTest(nine);
defautTest(ten);
int[] arr = {countA,countB,countC,countD};
for(int i = 0;i<4;i++) {
int max = arr[i];
for(int j = 1;j < 4;j++) {
if(arr[j]>max) {
arr[i] = arr[j];
arr[j] = max;
}
}
}
countMaxNumber = arr[0];
countMinNumber = arr[3];
if(countMaxNumber == countA) {
countMaxAn = staticA;
}
if(countMaxNumber == countC) {
countMaxAn = staticC;
}
if(countMaxNumber == countD) {
countMaxAn = staticD;
}
if(countMaxNumber == countB) {
countMaxAn = staticB;
}
if(countMinNumber == countA) {
countMinAn = staticA;
}
if(countMinNumber == countC) {
countMinAn = staticC;
}
if(countMinNumber == countD) {
countMinAn = staticD;
}
if(countMinNumber == countB) {
countMinAn = staticB;
}
}
//统计答案出现的次数
private void defautTest(String ch) {
switch (ch) {
case "A":
countA++;
break;
case "B":
countB++;
break;
case "C":
countC++;
break;
case "D":
countD++;
break;
}
}
//校验
private boolean checks() {
//验证第二题
if((two.equals(staticA)&&five.equals(staticC))||(two.equals(staticB)&&five.equals(staticD))||
(two.equals(staticC)&&five.equals(staticA))||(two.equals(staticD)&&five.equals(staticB))) {
//验证第三题
if((three.equals(six)&&three.equals(two)&&!three.equals(four)&&three.equals(staticD))||
(three.equals(six)&&three.equals(four)&&!three.equals(two)&&three.equals(staticC))||
(two.equals(six)&&two.equals(four)&&!two.equals(three)&&three.equals(staticA))||
(!two.equals(six)&&two.equals(four)&&two.equals(three)&&three.equals(staticB))) {
//验证第四题
if((one.equals(five)&&four.equals(staticA))||(one.equals(nine)&&four.equals(staticC))||(two.equals(seven)&&four.equals(staticB))||(six.equals(ten)&&four.equals(staticD))) {
//验证第五题
if((five.equals(eight)&&five.equals(staticA))||(five.equals(four)&&five.equals(staticB))||(five.equals(nine)&&five.equals(staticC))||(five.equals(seven)&&five.equals(staticD))) {
//验证第六题
if((eight.equals(two)&&eight.equals(four)&&six.equals(staticA))||(eight.equals(one)&&eight.equals(six)&&six.equals(staticB))||
(eight.equals(three)&&eight.equals(ten)&&six.equals(staticC))||(eight.equals(five)&&eight.equals(nine)&&six.equals(staticD))) {
//验证第八题
if(one.equals(staticA)) {
if(eight.equals(staticA)&&(seven.equals(staticC)||seven.equals(staticD))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticB)&&(five.equals(staticC)||five.equals(staticD))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticC)&&(two.equals(staticC)||two.equals(staticD))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticD)&&(ten.equals(staticC)||ten.equals(staticD))) {
boolean b = checkNine();
if(b) {
return true;
}
}
}
if(one.equals(staticB)) {
if(eight.equals(staticA)&&seven.equals(staticD)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticB)&&five.equals(staticD)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticC)&&two.equals(staticD)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticD)&&ten.equals(staticD)) {
boolean b = checkNine();
if(b) {
return true;
}
}
}
if(one.equals(staticC)) {
if(eight.equals(staticA)&&seven.equals(staticA)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&five.equals(staticA)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&two.equals(staticA)) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&ten.equals(staticA)) {
boolean b = checkNine();
if(b) {
return true;
}
}
}
if(one.equals(staticD)) {
if(eight.equals(staticA)&&(seven.equals(staticA)||seven.equals(staticB))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&(five.equals(staticA)||five.equals(staticB))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&(two.equals(staticA)||two.equals(staticB))) {
boolean b = checkNine();
if(b) {
return true;
}
}
if(eight.equals(staticA)&&(ten.equals(staticA)||ten.equals(staticB))) {
boolean b = checkNine();
if(b) {
return true;
}
}
}
}
}
}
}
}
//情况统计
countMaxNumber = 0;
countMinNumber = 0;
countA = 0;
countB = 0;
countC = 0;
countD = 0;
return false;
}
public boolean checkNine() {
//验证第九题
if((one.equals(six)&&((nine.equals(staticA)&&!five.equals(six))||(nine.equals(staticB)&&!five.equals(ten))||
(nine.equals(staticC)&&!five.equals(two))||(nine.equals(staticD)&&!five.equals(nine))))||
(!one.equals(six)&&((nine.equals(staticA)&&five.equals(six))||(nine.equals(staticB)&&five.equals(ten))||
(nine.equals(staticC)&&five.equals(two))||(nine.equals(staticD)&&five.equals(nine))))){
//验证第十题
if((ten.equals(staticA)&&(countMaxNumber-countMinNumber)==3)||(ten.equals(staticB)&&(countMaxNumber-countMinNumber)==2)||
(ten.equals(staticC)&&(countMaxNumber-countMinNumber)==4)||(ten.equals(staticD)&&(countMaxNumber-countMinNumber)==1)) {
//验证第七题
if((seven.equals(staticA)&&countMinAn.equals(staticC))||(seven.equals(staticB)&&countMinAn.equals(staticB))||
(seven.equals(staticC)&&countMinAn.equals(staticA))||(seven.equals(staticD)&&countMinAn.equals(staticD))){
return true;
}
}
}
return false;
}
控制台输出:
1
one=B
two=C
three=A
four=C
five=A
six=C
seven=D
eight=A
nine=B
ten=A
countMaxAn=A
countMinAn=D
----------------------------------------------
知乎上的答案:
我是菜鸟,我为自己代言。