Written in Java, who is a thief - exhaustive

The following functions: A, B, C, D four suspects, only 1 man thieves. At the trial, four honest or are likely to lie.

Four police officers interrogate suspects thieves. It is known that four of them are only a thief, but also know that the four are either honest in person, or always lying. In answer to the question police officers:
// A said: "B did not steal, Ding steal."
// B said:. "I did not steal, steal propionate is"
// propylene said: "A not steal, is B steal "
// Ding said:" I did not steal. "
// Please judged according to who is talking to the four thieves

Code:

public class 寻找窃贼
{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 公安人员审问四名窃贼嫌疑犯。已知,这四人当中仅有一名是窃贼,还知道这四人中每人要么是诚实的,要么总是说谎。在回答公安人员的问题中:
		// 甲说:“乙没有偷,是丁偷的。”
		// 乙说:“我没有偷,是丙偷的。”
		// 丙说:“甲没有偷,是乙偷的。”
		// 丁说:“我没有偷”
		// 请根据这四人的谈话判断谁是盗窃者
		int a, b, c, d;
		// 穷举
		// 真为0,假为1
		for (a = 0; a < 2; a++)
			for (b = 0; b < 2; b++)
				for (c = 0; c < 2; c++)
					for (d = 0; d < 2; d++)
					{
						if (b + d == 1 && b + c == 1 && a + b == 1
								&& a + b + c + d == 1)
						{// 成立找到窃贼
							if (a == 1)
								System.out.println("小偷是甲\n");
							if (b == 1)
								System.out.println("小偷是乙\n");
							if (c == 1)
								System.out.println("小偷是丙\n");
							if (d == 1)
								System.out.println("小偷是丁\n");
						}
					}
	}
}

Output:

小偷是乙
Released eight original articles · won praise 6 · views 175

Guess you like

Origin blog.csdn.net/Hackergu/article/details/105039772