importjava.util.Arrays;importjava.util.HashSet;importjava.util.Set;/*** 题目:如何看待2018年刑侦科推理试题
* 解法:穷举法。生成所有可能的答案组合(共1<<20种组合),将2-10题目作为筛选条件(第1题没有信息量)
*@authorHello stranger
**/
public classReasoning {/*** 生成数组
*@paramset*/
public static voidgenerate(Set set){//1号位
for (int b = 1; b < 5; b++) {//2
for (int c = 1; c < 5; c++) {//3
for (int d = 1; d < 5; d++) {//4
for (int e = 1; e < 5; e++) {//5
for (int f = 1; f < 5; f++) {//6
for (int g = 1; g < 5; g++) {//7
for (int h = 1; h < 5; h++) {//8
for (int i = 1; i < 5; i++) {//9
for (int j = 1; j < 5; j++) {//10
for (int k = 1; k < 5; k++) {int[] a = new int[11];
a[0] = 1;//判断是否符号条件标识位
put(a,1,b);
put(a,2,c);
put(a,3,d);
put(a,4,e);
put(a,5,f);
put(a,6,g);
put(a,7,h);
put(a,8,i);
put(a,9,j);
put(a,10,k);
set.add(a);
}
}
}
}
}
}
}
}
}
}
}/***
*@paramarr 数组
*@paramy 数组下标
*@paramx 数组的值(1、2、3、4 对应A、B、C、D)*/
public static void put(int[] a,int y,intx){
a[y]=x;
}//第二题
public static void second(int[] a){if(a[5] == 3 && a[2] == 1){}else if(a[5] == 4 && a[2] == 2){}else if(a[5] == 1 && a[2] == 3){}else if(a[5] == 2 && a[2] == 4){}else{
a[0] = 0;
}
}//第三题
public static void third(int[] a){if(a[2]==a[3] && a[3]==a[4] && a[3]!=a[6] && a[3] == 2){}//234答案相同
else if(a[2]==a[3] && a[3]==a[6] && a[3]!=a[4] && a[3] == 4){}//236答案相同
else if(a[2]==a[4] && a[4]==a[6] && a[4]!=1 && a[3] == 1){}//246答案相同
else if(a[3]==a[4] && a[4]==a[6] && a[3]!=a[2] && a[3] == 3){}//346答案相同
else{//不符合条件
a[0] = 0;
}
}//第四题
public static void fourth(int[] a){if(a[1] == a[5] && a[4] == 1){}else if(a[2] == a[7] && a[4] == 2){}else if(a[1] == a[9] && a[4] == 3){}else if(a[6] == a[10] && a[4] == 4){}else{
a[0] = 0;
}
}//第五题
public static void fifth(int[] a){if(a[5] == a[8] && a[5] == 1){}else if(a[5] == a[4] && a[5] == 2){}else if(a[5] == a[9] && a[5] == 3){}else if(a[5] == a[7] && a[5] == 4){}else{
a[0] = 0;
}
}//第六题
public static void sixth(int[] a){if(a[2] == a[8] && a[4] == a[8] && a[6] == 1){}else if(a[1] == a[8] && a[6] == a[8] && a[6] == 2){}else if(a[3] == a[8] && a[10] == a[8] && a[6] == 3){}else if(a[5] == a[8] && a[9] == a[8] && a[6] == 4){}else{
a[0] = 0;
}
}//第七题//这10题中被选中次数最少的
public static void seventh(int[] a){int min = count(a, "min");if(min == 1 && a[7] == 3){}else if(min == 2 && a[7] == 2){}else if(min == 3 && a[7] == 1){}else if(min == 4 && a[7] == 4){}else{
a[0] = 0;
}
}//第八题
public static void eighth(int[] a){if(a[1]-1 != a[7] || a[1]+1 != a[7] && a[8] == 1){}else if(a[1]-1 != a[5] || a[1]+1 != a[5] && a[8] == 2){}else if(a[1]-1 != a[2] || a[1]+1 != a[2] && a[8] == 3){}else if(a[1]-1 != a[10] || a[1]+1 != a[10] && a[8] == 4){}else{
a[0] = 0;
}
}//第九题
public static void nineth(int[] a){if(a[1] == a[6]){if(a[6] != a[5] && a[9] == 1){}else if(a[10] != a[5] && a[9] == 2){}else if(a[2] != a[5] && a[9] == 3){}else if(a[9] != a[5] && a[9] == 4){}else{
a[0] = 0;
}
}else{if(a[6] == a[5] && a[9] == 1){}else if(a[10] == a[5] && a[9] == 2){}else if(a[2] == a[5] && a[9] == 3){}else if(a[9] == a[5] && a[9] == 4){}else{
a[0] = 0;
}
}
}//第十题
public static void tenth(int[] a){int sub = count(a, "sub");if(sub == 3 && a[10] == 1){}else if(sub == 2 && a[10] == 2){}else if(sub == 4 && a[10] == 3){}else if(sub == 1 && a[10] == 4){}else{
a[0] = 0;
}
}/*** TODO:统计返回最大出现次数与最小出现次数的差,或返回出现次数最小的选项
*@parama 数组
*@paramm "sub"返回次数差,否则返回出现次数最小的选项
*@return
*/
public static int count(int[] a,String m){int[] num = new int[5];for(int i=1; i<11; i++){if(a[i] == 1){
num[1] ++;
}else if(a[i] == 2){
num[2]++;
}else if(a[i] == 3){
num[3]++;
}else{
num[4]++;
}
}int max = Math.max(num[1],
Math.max(num[2],
Math.max(num[3], num[4])));int min = Math.min(num[1],
Math.min(num[2],
Math.min(num[3], num[4])));if("sub".equals(m)){return max -min;
}else{if(min == num[1]){return 1;
}else if(min == num[2]){return 2;
}else if(min == num[3]){return 3;
}else{return 4;
}
}
}public static voidmain(String[] args) {int num = 0;
HashSet set = new HashSet();
generate(set);for(int[] a : set){
second(a);
third(a);
fourth(a);
fifth(a);
sixth(a);
seventh(a);
eighth(a);
nineth(a);
tenth(a);//输出符合条件的答案
if(a[0] == 1){for(int j : Arrays.copyOfRange(a, 1, a.length)){char result = '0';if(j==1) {
result= 'A';
}else if(j==2) {
result= 'B';
}else if(j==3) {
result= 'C';
}else if(j==4) {
result= 'D';
}
System.out.print(result+",");
}
System.out.println();
}
}
}
}