一个java趣味题
前几天发给一个朋友一道编程趣味题,结果第二天他给我了这个题目的另一种解法,于是我就想将这两种解法都贴出来,算是没事时消磨时间的点心题目描述:
/**
* 某地刑侦大队对涉及六个嫌疑人的一桩疑案进行分析: <br>
* A、B至少有一人作案;<br>
* A、E、F三人中至少有两人参与作案;<br>
* A、D不可能是同案犯;<br>
* B、C或同时作案,或与本案无关; C、D中有且仅有一人作案;<br>
* 如果D没有参与作案,则E也不可能参与作案。<br>
* 试编一程序,将作案人找出来。
*/
1:
public class T {
public static void main(String[] args) {
String[] persons = { "A", "B", "C", "D", "E", "F" };
int A = 'A';
System.out.println("f : " + A);
for (int i = 0; i <= 'F'; i++) {
int t = i;
int num[] = new int;
int j = 5;
for (; j >= 0; j--) {// 换成二进制数码,保存在一个6个元素的一维数组中
if (t / 2 > 0) {
num = t % 2;
t = t / 2;
} else {
num = t;
break;
}
}
// num--num代表A--F
// 代表AB至少有一个人作案
if (num + num >= 1) {
// AEF 至少2个人作案
if (num + num + num >= 2) {
// AD 不是同案犯。但可能都没作案或其中一个作案
if (num + num <= 1) {
// BC 同时作案,或者同时没作案
if ((num + num == 0 || num + num == 2)) {
// CD肯定有一个人作案了
if (num + num == 1) {
// 如果D没作案且E也没作案,或者 D作案了了
if (num == 0 && num == 0 || num == 1)
for (int index = 0; index < num.length; index++) {
if (num == 1) {
System.out.print(persons);
}
}
}
}
}
}
}
}
}
}
2:
public class T2 {
public static int pp = 0;
public static void main(String args[]) {
int q[] = new int[] { 1, 2, 4, 8, 16, 32 };
boolean xj[] = new boolean;
for (int i = 0; i <= 64; i++) {
for (int j = 0; j < 6; j++) {
if ((q & i) > 0)
xj = true;
else
xj = false;
}
va(xj);
}
}
public static void va(boolean xj[]) {
if ((xj || xj)
&& ((xj && xj) || (xj && xj) || (xj && xj) || (xj
&& xj && xj)) && !(xj && xj)
&& ((xj && xj) || !(xj || xj))
&& ((xj || xj) && !(xj && xj))
&& ((xj && xj) || !(xj || xj))) {
for (int a = 0; a < 6; a++) {
System.out.print(xj + " ");
}
System.out.println("" + pp++);
}
}
}
页:
[1]