关于六个嫌疑人的问题

package bag;

import com.sun.accessibility.internal.resources.accessibility;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;

public class CriminalTest {

/**
* 某地刑侦大队对涉及六个嫌疑人的一桩疑案进行分析: <br>
* A、B至少有一人作案;<br>
* A、E、F三人中至少有两人参与作案;<br>
* A、D不可能是同案犯;<br>
* B、C或同时作案,或与本案无关; C、D中有且仅有一人作案;<br>
* 如果D没有参与作案,则E也不可能参与作案。<br>
* 试编一程序,将作案人找出来。
*
* @author 李佳佳
*
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array = new int[64][6];
int tmep = 0;
int num = 0;
// 生成64个数组集合,每个数组里面放六个元素,分别代表六个罪犯的真假,由于每个罪犯可能为真或者假,所以一共有2的6次方,为64种可能性

for (int i = 0; i < 64; i++) {
tmep = i;
num = 0;
//将每种组合看作是一个数的二进制码:0-63
while (tmep != 0) {
array[i][num] = tmep - tmep / 2 * 2;
num++;
if (tmep > 1) {
tmep = tmep / 2;
} else {
tmep = 0;
}
}
int linshi = 0;
for (int o = 0; o < 3; o++) {
linshi = array[i][o];
array[i][o] = array[i][5 - o];
array[i][5 - o] = linshi;
}
}
//依次遍历每个数组,根据条件就知道了有没有罪犯了
for (int test = 0; test < 64; test++) {
if ((array[test][0] + array[test][1]) >= 1
& (array[test][0] + array[test][4] + array[test][5] >= 2)
& (array[test][0] + array[test][3] < 2)
& (array[test][1] + array[test][2] == 2 || array[test][1]
+ array[test][2] == 2)
& (array[test][2] + array[test][3] == 1)
& (array[test][1] + array[test][2] == 2))

{
//輸出合理的結果
if (array[test][3]==0){
if (array[test][4]==0){
for (int out=0;out<6;out++){
System.out.print(array[test][out]+";");
}
}
}else {
for (int out=0;out<6;out++){
System.out.print(array[test][out]+";");
}
}

}

}

}

}
结果:1;1;1;0;0;1; A.B.C.D.E.F犯人:A.B.C.F
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值