先上题目
这是网上非常火的刑侦科推理测试题,当时看到感觉很有意思。想着怎么解出来,尝试了一段时间以后发现自己完全hold不住呐!刹那间,灵关一现,用程序实现应该很快哦!说干就干(程序算出来以后去网上搜答案,才发现已经有人用编程搞定了)
思路
首先想到的当然是穷举法;
先利用数组存储其中一种可能的排列,再把排列放到List之类的里面。
利用每道题的条件进行过滤各种组合,不符合的就从List中去除。(虽然标答只有一种,可以换成满足条件直接输出)
根据每道题的条件写过滤函数
大功告成
正式开始码代码了
public class Infer {
private List<Integer[]> list;
public Infer() {
list = new ArrayList<>();
}
public static void main(String[] args) {
Infer obj = new Infer();
obj.initData();
System.out.println("数据初始化完成,共" + obj.list.size() + "种情况");
for (Integer[] integers : obj.list) {
if (!obj.Filter1(integers)) {
continue;
}
if (!obj.Filter2(integers)) {
continue;
}
if (!obj.Filter3(integers)) {
continue;
}
if (!obj.Filter4(integers)) {
continue;
}
if (!obj.Filter5(integers)) {
continue;
}
if (!obj.Filter6(integers)) {
continue;
}
if (!obj.Filter7(integers)) {
continue;
}
if (!obj.Filter8(integers)) {
continue;
}
if (!obj.Filter9(integers)) {
continue;
}
if (!obj.Filter10(integers)) {
continue;
}
obj.printArr(integers);
}
}
public boolean Filter1(Integer[] integers) {
return true;
}
public boolean Filter2(Integer[] integers) {
int answer2 = integers[1];
int answer5 = integers[4];
switch (answer2) {
case 0:
if (answer5 == 2) {
return true;
}
break;
case 1:
if (answer5 == 3) {
return true;
}
break;
case 2:
if (answer5 == 0) {
return true;
}
break;
case 3:
if (answer5 == 1) {
return true;
}
break;
default:
return false;
}
return false;
}
public boolean Filter3(Integer[] integers) {
int answer3 = integers[2];
int answer6 = integers[5];
int answer2 = integers[1];
int answer4 = integers[3];
switch (answer3) {
case 0:
if (answer3 == answer6 || answer3 == answer2 || answer3 == answer4) {
return false;
} else {
return true;
}
case 1:
if (answer6 == answer3 || answer6 == answer2 || answer6 == answer4) {
return false;
} else {
return true;
}
case 2:
if (answer2 == answer3 || answer2 == answer6 || answer2 == answer4) {
return false;
} else {
return true;
}
case 3:
if (answer4 == answer3 || answer4 == answer6 || answer4 == answer2) {
return false;
} else {
return true;
}
default:
return false;
}
}
public boolean Filter4(Integer[] integers) {
int answer4 = integers[3];
switch (answer4) {
case 0:
if (integers[0] == integers[4]) {
return true;
} else {
return false;
}
case 1:
if (integers[1] == integers[6]) {
return true;
} else {
return false;
}
case 2:
if (integers[0] == integers[8]) {
return true;
} else {
return false;
}
case 3:
if (integers[5] == integers[9]) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter5(Integer[] integers) {
int answer5 = integers[4];
switch (answer5) {
case 0:
if (answer5 == integers[7]) {
return true;
} else {
return false;
}
case 1:
if (answer5 == integers[3]) {
return true;
} else {
return false;
}
case 2:
if (answer5 == integers[8]) {
return true;
} else {
return false;
}
case 3:
if (answer5 == integers[6]) {
return true;
} else {
return false;
}
default:
return false;
}
}
public boolean Filter6(Integer[] integers) {
int answer6 = integers[5];
int answer8 = integers[7];
switch (answer6) {
case 0:
if (answer8 == integers[1] && answer8 == integers[3]) {
return true;
} else {
return false;
}
case 1:
if (answer8 == integers[0] && answer8 == integers[5]) {
return true;
} else {
return false;
}
case 2:
if (answer8 == integers[2] && answer8 == integers[9]) {
return true;
} else {
return false;
}
case 3:
if (answer8 == integers[4] && answer8 == integers[8]) {
return true;
} else {
return false;
}
default:
return false;
}
}
public boolean Filter7(Integer[] integers) {
int answer7 = integers[6];
Map<Integer, Integer> count = count(integers);
Integer count0 = count.get(0);
Integer count1 = count.get(1);
Integer count2 = count.get(2);
Integer count3 = count.get(3);
if (count0 == null) {
count0 = 0;
}
if (count1 == null) {
count1 = 0;
}
if (count2 == null) {
count2 = 0;
}
if (count3 == null) {
count3 = 0;
}
Integer countmin = Math.min(count0, count1);
countmin = Math.min(countmin, count2);
countmin = Math.min(countmin, count3);
int minno=-1;
if (count0==countmin) {
minno=0;
}else if (count1==countmin) {
minno=1;
}else if (count2==countmin) {
minno=2;
}else if (count3==countmin) {
minno=3;
}
switch (answer7) {
case 0:
if (minno == 2) {
return true;
} else {
return false;
}
case 1:
if (minno == 1) {
return true;
} else {
return false;
}
case 2:
if (minno == 0) {
return true;
} else {
return false;
}
case 3:
if (minno == 3) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter8(Integer[] integers) {
int answer8 = integers[7];
int answer1 = integers[0];
switch (answer8) {
case 0:
if (answer1 + 1 == integers[6] || answer1 - 1 == integers[6]) {
return false;
} else {
return true;
}
case 1:
if (answer1 + 1 == integers[4] || answer1 - 1 == integers[4]) {
return false;
} else {
return true;
}
case 2:
if (answer1 + 1 == integers[1] || answer1 - 1 == integers[1]) {
return false;
} else {
return true;
}
case 3:
if (answer1 + 1 == integers[9] || answer1 - 1 == integers[9]) {
return false;
} else {
return true;
}
default:
break;
}
return false;
}
public boolean Filter9(Integer[] integers) {
int answer8 = integers[8];
boolean b16=false;
if (integers[0] == integers[5]) {
b16=true;
}
switch (answer8) {
case 0:
boolean b56 = integers[4] == integers[5] ? true : false;
if (b16 != b56) {
return true;
} else {
return false;
}
case 1:
boolean b510 = integers[4] == integers[9] ? true : false;
if (b16 != b510) {
return true;
} else {
return false;
}
case 2:
boolean b52 = integers[4] == integers[1] ? true : false;
if (b16 != b52) {
return true;
} else {
return false;
}
case 3:
boolean b59 = integers[4] == integers[8] ? true : false;
if (b16 != b59) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter10(Integer[] integers) {
int answer10 = integers[9];
Map<Integer, Integer> count = count(integers);
Integer count0 = count.get(0);
Integer count1 = count.get(1);
Integer count2 = count.get(2);
Integer count3 = count.get(3);
if (count0 == null) {
count0 = 0;
}
if (count1 == null) {
count1 = 0;
}
if (count2 == null) {
count2 = 0;
}
if (count3 == null) {
count3 = 0;
}
Integer countmin = Math.min(count0, count1);
countmin = Math.min(countmin, count2);
countmin = Math.min(countmin, count3);
Integer countmax = Math.max(count0, count1);
countmax = Math.max(countmax, count2);
countmax = Math.max(countmax, count3);
Integer maxmin = countmax - countmin;
switch (answer10) {
case 0:
if (maxmin == 3) {
return true;
} else {
return false;
}
case 1:
if (maxmin == 2) {
return true;
} else {
return false;
}
case 2:
if (maxmin == 4) {
return true;
} else {
return false;
}
case 3:
if (maxmin == 1) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public void printArr(Integer[] integers) {
System.out.print("解:");
for (int i = 0; i < integers.length; i++) {
switch (integers[i]) {
case 0:
System.out.print("A");
break;
case 1:
System.out.print("B");
break;
case 2:
System.out.print("C");
break;
case 3:
System.out.print("D");
break;
default:
break;
}
if (i!=integers.length-1) {
System.out.print(",");
}
}
System.out.println();
}
public static Map<Integer, Integer> count(Integer[] integers) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < integers.length; i++) {
if (!map.containsKey(integers[i])) {
map.put(integers[i], 1);
} else {
map.put(integers[i], map.get(integers[i]) + 1);
}
}
return map;
}
public void initData() {
int optionnub = 4;
for (int a = 0; a < optionnub; a++) {
for (int b = 0; b < optionnub; b++) {
for (int c = 0; c < optionnub; c++) {
for (int d = 0; d < optionnub; d++) {
for (int e = 0; e < optionnub; e++) {
for (int f = 0; f < optionnub; f++) {
for (int g = 0; g < optionnub; g++) {
for (int h = 0; h < optionnub; h++) {
for (int i = 0; i < optionnub; i++) {
for (int j = 0; j < optionnub; j++) {
Integer[] integers = { a, b, c, d, e, f, g, h, i, j };
list.add(integers);
}
}
}
}
}
}
}
}
}
}
}
}
完全是直接写的,没做任何优化!
这是网上非常火的刑侦科推理测试题,当时看到感觉很有意思。想着怎么解出来,尝试了一段时间以后发现自己完全hold不住呐!刹那间,灵关一现,用程序实现应该很快哦!说干就干(程序算出来以后去网上搜答案,才发现已经有人用编程搞定了)
首先想到的当然是穷举法;
先利用数组存储其中一种可能的排列,再把排列放到List之类的里面。
利用每道题的条件进行过滤各种组合,不符合的就从List中去除。(虽然标答只有一种,可以换成满足条件直接输出)
根据每道题的条件写过滤函数
大功告成
public class Infer {
private List<Integer[]> list;
public Infer() {
list = new ArrayList<>();
}
public static void main(String[] args) {
Infer obj = new Infer();
obj.initData();
System.out.println("数据初始化完成,共" + obj.list.size() + "种情况");
for (Integer[] integers : obj.list) {
if (!obj.Filter1(integers)) {
continue;
}
if (!obj.Filter2(integers)) {
continue;
}
if (!obj.Filter3(integers)) {
continue;
}
if (!obj.Filter4(integers)) {
continue;
}
if (!obj.Filter5(integers)) {
continue;
}
if (!obj.Filter6(integers)) {
continue;
}
if (!obj.Filter7(integers)) {
continue;
}
if (!obj.Filter8(integers)) {
continue;
}
if (!obj.Filter9(integers)) {
continue;
}
if (!obj.Filter10(integers)) {
continue;
}
obj.printArr(integers);
}
}
public boolean Filter1(Integer[] integers) {
return true;
}
public boolean Filter2(Integer[] integers) {
int answer2 = integers[1];
int answer5 = integers[4];
switch (answer2) {
case 0:
if (answer5 == 2) {
return true;
}
break;
case 1:
if (answer5 == 3) {
return true;
}
break;
case 2:
if (answer5 == 0) {
return true;
}
break;
case 3:
if (answer5 == 1) {
return true;
}
break;
default:
return false;
}
return false;
}
public boolean Filter3(Integer[] integers) {
int answer3 = integers[2];
int answer6 = integers[5];
int answer2 = integers[1];
int answer4 = integers[3];
switch (answer3) {
case 0:
if (answer3 == answer6 || answer3 == answer2 || answer3 == answer4) {
return false;
} else {
return true;
}
case 1:
if (answer6 == answer3 || answer6 == answer2 || answer6 == answer4) {
return false;
} else {
return true;
}
case 2:
if (answer2 == answer3 || answer2 == answer6 || answer2 == answer4) {
return false;
} else {
return true;
}
case 3:
if (answer4 == answer3 || answer4 == answer6 || answer4 == answer2) {
return false;
} else {
return true;
}
default:
return false;
}
}
public boolean Filter4(Integer[] integers) {
int answer4 = integers[3];
switch (answer4) {
case 0:
if (integers[0] == integers[4]) {
return true;
} else {
return false;
}
case 1:
if (integers[1] == integers[6]) {
return true;
} else {
return false;
}
case 2:
if (integers[0] == integers[8]) {
return true;
} else {
return false;
}
case 3:
if (integers[5] == integers[9]) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter5(Integer[] integers) {
int answer5 = integers[4];
switch (answer5) {
case 0:
if (answer5 == integers[7]) {
return true;
} else {
return false;
}
case 1:
if (answer5 == integers[3]) {
return true;
} else {
return false;
}
case 2:
if (answer5 == integers[8]) {
return true;
} else {
return false;
}
case 3:
if (answer5 == integers[6]) {
return true;
} else {
return false;
}
default:
return false;
}
}
public boolean Filter6(Integer[] integers) {
int answer6 = integers[5];
int answer8 = integers[7];
switch (answer6) {
case 0:
if (answer8 == integers[1] && answer8 == integers[3]) {
return true;
} else {
return false;
}
case 1:
if (answer8 == integers[0] && answer8 == integers[5]) {
return true;
} else {
return false;
}
case 2:
if (answer8 == integers[2] && answer8 == integers[9]) {
return true;
} else {
return false;
}
case 3:
if (answer8 == integers[4] && answer8 == integers[8]) {
return true;
} else {
return false;
}
default:
return false;
}
}
public boolean Filter7(Integer[] integers) {
int answer7 = integers[6];
Map<Integer, Integer> count = count(integers);
Integer count0 = count.get(0);
Integer count1 = count.get(1);
Integer count2 = count.get(2);
Integer count3 = count.get(3);
if (count0 == null) {
count0 = 0;
}
if (count1 == null) {
count1 = 0;
}
if (count2 == null) {
count2 = 0;
}
if (count3 == null) {
count3 = 0;
}
Integer countmin = Math.min(count0, count1);
countmin = Math.min(countmin, count2);
countmin = Math.min(countmin, count3);
int minno=-1;
if (count0==countmin) {
minno=0;
}else if (count1==countmin) {
minno=1;
}else if (count2==countmin) {
minno=2;
}else if (count3==countmin) {
minno=3;
}
switch (answer7) {
case 0:
if (minno == 2) {
return true;
} else {
return false;
}
case 1:
if (minno == 1) {
return true;
} else {
return false;
}
case 2:
if (minno == 0) {
return true;
} else {
return false;
}
case 3:
if (minno == 3) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter8(Integer[] integers) {
int answer8 = integers[7];
int answer1 = integers[0];
switch (answer8) {
case 0:
if (answer1 + 1 == integers[6] || answer1 - 1 == integers[6]) {
return false;
} else {
return true;
}
case 1:
if (answer1 + 1 == integers[4] || answer1 - 1 == integers[4]) {
return false;
} else {
return true;
}
case 2:
if (answer1 + 1 == integers[1] || answer1 - 1 == integers[1]) {
return false;
} else {
return true;
}
case 3:
if (answer1 + 1 == integers[9] || answer1 - 1 == integers[9]) {
return false;
} else {
return true;
}
default:
break;
}
return false;
}
public boolean Filter9(Integer[] integers) {
int answer8 = integers[8];
boolean b16=false;
if (integers[0] == integers[5]) {
b16=true;
}
switch (answer8) {
case 0:
boolean b56 = integers[4] == integers[5] ? true : false;
if (b16 != b56) {
return true;
} else {
return false;
}
case 1:
boolean b510 = integers[4] == integers[9] ? true : false;
if (b16 != b510) {
return true;
} else {
return false;
}
case 2:
boolean b52 = integers[4] == integers[1] ? true : false;
if (b16 != b52) {
return true;
} else {
return false;
}
case 3:
boolean b59 = integers[4] == integers[8] ? true : false;
if (b16 != b59) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public boolean Filter10(Integer[] integers) {
int answer10 = integers[9];
Map<Integer, Integer> count = count(integers);
Integer count0 = count.get(0);
Integer count1 = count.get(1);
Integer count2 = count.get(2);
Integer count3 = count.get(3);
if (count0 == null) {
count0 = 0;
}
if (count1 == null) {
count1 = 0;
}
if (count2 == null) {
count2 = 0;
}
if (count3 == null) {
count3 = 0;
}
Integer countmin = Math.min(count0, count1);
countmin = Math.min(countmin, count2);
countmin = Math.min(countmin, count3);
Integer countmax = Math.max(count0, count1);
countmax = Math.max(countmax, count2);
countmax = Math.max(countmax, count3);
Integer maxmin = countmax - countmin;
switch (answer10) {
case 0:
if (maxmin == 3) {
return true;
} else {
return false;
}
case 1:
if (maxmin == 2) {
return true;
} else {
return false;
}
case 2:
if (maxmin == 4) {
return true;
} else {
return false;
}
case 3:
if (maxmin == 1) {
return true;
} else {
return false;
}
default:
break;
}
return false;
}
public void printArr(Integer[] integers) {
System.out.print("解:");
for (int i = 0; i < integers.length; i++) {
switch (integers[i]) {
case 0:
System.out.print("A");
break;
case 1:
System.out.print("B");
break;
case 2:
System.out.print("C");
break;
case 3:
System.out.print("D");
break;
default:
break;
}
if (i!=integers.length-1) {
System.out.print(",");
}
}
System.out.println();
}
public static Map<Integer, Integer> count(Integer[] integers) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < integers.length; i++) {
if (!map.containsKey(integers[i])) {
map.put(integers[i], 1);
} else {
map.put(integers[i], map.get(integers[i]) + 1);
}
}
return map;
}
public void initData() {
int optionnub = 4;
for (int a = 0; a < optionnub; a++) {
for (int b = 0; b < optionnub; b++) {
for (int c = 0; c < optionnub; c++) {
for (int d = 0; d < optionnub; d++) {
for (int e = 0; e < optionnub; e++) {
for (int f = 0; f < optionnub; f++) {
for (int g = 0; g < optionnub; g++) {
for (int h = 0; h < optionnub; h++) {
for (int i = 0; i < optionnub; i++) {
for (int j = 0; j < optionnub; j++) {
Integer[] integers = { a, b, c, d, e, f, g, h, i, j };
list.add(integers);
}
}
}
}
}
}
}
}
}
}
}
}
完全是直接写的,没做任何优化!