不用代码作简单的逻辑推理很容易额………………代码嘛,刚才写了一个………………public class test { public static void main(String[] args) { for (int i = 0; i < 1 << 6; i++) if (condition1(i) && condition2(i) && condition3(i) && condition4(i) && condition5(i) && condition6(i)) print(i); }
boolean a = suspects[0]; boolean b = suspects[1]; boolean c = suspects[2]; boolean d = suspects[3]; boolean e = suspects[4]; boolean f = suspects[5];
if (!(a && b)) return;
if ((a == false && e == false) || (a == false && f == false) || (f == false && e == false)) return;
if (a && d) return;
if (b != c) return;
if (c == d) return;
if ((d == false) && (e == true)) return;
System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d + " E:" + e + " F:" + f); }
public static void main(String[] args) { Q police = new Q(); police.allPossible = new ArrayList<boolean[]>(); police.suspects = new boolean[6]; police.buildAll(6);
for (boolean[] suspects : police.allPossible) { police.trial(suspects); } } }
boolean a = suspects[0]; boolean b = suspects[1]; boolean c = suspects[2]; boolean d = suspects[3]; boolean e = suspects[4]; boolean f = suspects[5];
if (!(a && b)) return;
if ((a == false && e == false) || (a == false && f == false) || (f == false && e == false)) return;
if (a && d) return;
if (b != c) return;
if (c == d) return;
if ((d == false) && (e == true)) return;
System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d + " E:" + e + " F:" + f); }
public static void main(String[] args) { Q police = new Q(); police.allPossible = new ArrayList<boolean[]>(); police.suspects = new boolean[6]; police.buildAll(6);
for (boolean[] suspects : police.allPossible) { police.trial(suspects); } } }
public static void main(String[] args) {
for (int i = 0; i < 1 << 6; i++)
if (condition1(i) && condition2(i) && condition3(i) && condition4(i) && condition5(i) && condition6(i))
print(i);
}
private static boolean condition1(int i) {
return ((i & 1) | ((i >> 1) & 1)) != 0;
}
private static boolean condition2(int i) {
return ((i & 1) + ((i >> 4) & 1) + ((i >> 5) & 1)) >= 2;
}
private static boolean condition3(int i) {
return ((i & 1) & ((i >> 3) & 1)) == 0;
}
private static boolean condition4(int i) {
return (((i >> 1) & 1) ^ ((i >> 2) & 1)) == 0;
}
private static boolean condition5(int i) {
return (((i >> 2) & 1) ^ ((i >> 3) & 1)) == 1;
}
private static boolean condition6(int i) {
return !(((i >> 3) & 1) == 0 && ((i >> 4) & 1) == 1);
}
private static void print(int n) {
System.out.print("可能的凶手组合: ");
for (int i = 0; i < 6; i++)
if (((n >> i) & 1) != 0)
System.out.print((char)('A' + i));
System.out.println();
}
}
简单的说,就是把一个6位2进制数的6个位看成是6个人是否犯罪,从低位到高位分别是ABCDEF,1表示犯罪,举个例子110101就代表ACEF是犯人
那么就把问题转化为位运算来判断
* @param args
*/
//false表示没作案,true表示作案
private static boolean A,B,C,D,E,F;
public static void main(String[] args) {
// TODO Auto-generated method stub
init();
}
private static void init() {
for(int i=0;i<=1;i++){
for(int j=0;j<=1;j++){
for(int k=0;k<=1;k++){
for(int l=0;l<=1;l++){
for(int m=0;m<=1;m++){
for(int n=0;n<=1;n++){
A=i==0?false:true;
B=j==0?false:true;
C=k==0?false:true;
D=l==0?false:true;
E=m==0?false:true;
F=n==0?false:true;
if(validate()){
printOK();
System.out.println("------------------");
return;
}
}
}
}
}
}
}
System.out.println("条件不满足,神仙也找不到作案人了");
}
private static void printOK() {
System.out.println("A,B,C,D,E,F作案情况分别是\n");
System.out.println("A:"+format(A));
System.out.println("B:"+format(B));
System.out.println("C:"+format(C));
System.out.println("D:"+format(D));
System.out.println("E:"+format(E));
System.out.println("F:"+format(F));
}
private static String format(boolean v) {
return v==true?"作案\n":"未作案\n";
}
private static boolean validate() {
//A、B中至少1人作案
if(!A&&!B) return false;
//AEF3人中至少2个人参加
if(A&&(!E&&!F)) return false;
if(E&&(!A&&!F)) return false;
if(F&&(!E&&!A)) return false;
if(!F&&!E&&!A) return false;
// AD不可能是同案犯
if(A&&D) return false;
//BC或同时作案,或者未参加
if((B||C)&&(B!=C)) return false;
//CD中有且仅有一个
if(C==D) return false;
if(!D&&E) return false;
return true;
}}
public static void main(String [] args) {
for(int a=0; a<2; a++) {
for(int b=0; b<2; b++) {
for(int c=0; c<2; c++) {
for(int d=0; d<2; d++) {
for(int e=0; e<2; e++) {
for(int f=0; f<2; f++) {
//(1) A、B中至少1人作案;
if((a+b)==0) {
continue;
}
//(2) AEF3人中至少2个人参加;
if((a+e+f)<2) {
continue;
}
//(3) AD不可能是同案犯;
if((a+d)==2) {
continue;
}
//(4) BC或同时作案,或者未参加;
if((b+c)==1) {
continue;
}
//(5) CD中有且仅有一个;
if((c+d)!=1) {
continue;
}
//(6) 若D没有作案,则E也不可能参与;
if((d==0)&&(e==1)) {
continue;
}
System.out.println("A" + check(a));
System.out.println("B" + check(b));
System.out.println("C" + check(c));
System.out.println("D" + check(d));
System.out.println("E" + check(e));
System.out.println("F" + check(f));
}
}
}
}
}
}
}
static String check(int i) {
return i==0 ? "没有作案" : "作案了";
}
}“1”--作案了;
“0”--没作案。结果:ABCF作案了,DE没有
private boolean[] suspects;
private ArrayList<boolean[]> allPossible;
void buildAll(int i) {
if (--i == 0) {
suspects[i] = true;
allPossible.add(suspects.clone());
suspects[i] = false;
allPossible.add(suspects.clone());
} else {
suspects[i] = true;
buildAll(i);
suspects[i] = false;
buildAll(i);
}
}
public void trial(boolean[] suspects) {
boolean a = suspects[0];
boolean b = suspects[1];
boolean c = suspects[2];
boolean d = suspects[3];
boolean e = suspects[4];
boolean f = suspects[5];
if (!(a && b))
return;
if ((a == false && e == false) || (a == false && f == false)
|| (f == false && e == false))
return;
if (a && d)
return;
if (b != c)
return;
if (c == d)
return;
if ((d == false) && (e == true))
return;
System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d
+ " E:" + e + " F:" + f);
}
public static void main(String[] args) {
Q police = new Q();
police.allPossible = new ArrayList<boolean[]>();
police.suspects = new boolean[6];
police.buildAll(6);
for (boolean[] suspects : police.allPossible) {
police.trial(suspects);
}
}
}
private boolean[] suspects;
private ArrayList<boolean[]> allPossible;
void buildAll(int i) {
if (--i == 0) {
suspects[i] = true;
allPossible.add(suspects.clone());
suspects[i] = false;
allPossible.add(suspects.clone());
} else {
suspects[i] = true;
buildAll(i);
suspects[i] = false;
buildAll(i);
}
}
public void trial(boolean[] suspects) {
boolean a = suspects[0];
boolean b = suspects[1];
boolean c = suspects[2];
boolean d = suspects[3];
boolean e = suspects[4];
boolean f = suspects[5];
if (!(a && b))
return;
if ((a == false && e == false) || (a == false && f == false)
|| (f == false && e == false))
return;
if (a && d)
return;
if (b != c)
return;
if (c == d)
return;
if ((d == false) && (e == true))
return;
System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d
+ " E:" + e + " F:" + f);
}
public static void main(String[] args) {
Q police = new Q();
police.allPossible = new ArrayList<boolean[]>();
police.suspects = new boolean[6];
police.buildAll(6);
for (boolean[] suspects : police.allPossible) {
police.trial(suspects);
}
}
}