Dr. puzzle riddle Java- brute force method

Dr. puzzle riddle Java- brute force method

Honest family and family is lying island from two different ethnic groups, honest family who always tell the truth, lied family who never tell lies. Dr. riddles was a clever man, he wants to judge people encounter from which the nation.
Dr. riddles encountered three men, knowing that they may have come from an honest family or lying family. In order to investigate the three men is what family, Dr. were asked their questions, this is their conversation: the first person to ask: "What are you family?" A: "There are two of us from an honest family . "the second man said:" Do not talk nonsense, we have only one in three people are honest family, "the second person after the third person listening to the words said:".. Yes, there is only one honest family, "
please according to his they are the answer to determine which family.

Code:

public class 真假族
{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 迷语博士遇到三个人,知道他们可能是来自诚实族或说谎族的。
		// 为了调查这三个人是什么族的,博士分别问了他们的问题,这是他们的对话:
		// 问第一个人:“你们是什么族?”,答:“我们之中有两个来自诚实族。
		// ”第二个人说:“不要胡说,我们三个人中只有一个是诚实族的。
		// ”第三个人听了第二个人的话后说:“对,就是只有一个诚实族的。”

		// 暴力破解法
		int a, b, c;
		boolean A, B, C;
		for (a = 0; a <= 1; a++)/* 穷举每个人是说谎还是诚实的全部情况 */
		{
			if (a == 0)
			{
				A = false;/* 说谎:false 诚实:true */
			} else
			{
				A = true;
			}
			for (b = 0; b <= 1; b++)
			{
				if (b == 0)
				{
					B = false;/* 说谎:false 诚实:true */
				} else
				{
					B = true;
				}
				for (c = 0; c <= 1; c++)
				{

					if (c == 0)
					{
						C = false;/* 说谎:false 诚实:true */
					} else
					{
						C = true;
					}
					if (((A && a + b + c == 2) || (!A && a + b + c != 2))
							&& ((B && a + b + c == 1) || (!B && a + b + c != 1))
							&& ((C && a + b + c == 1) || (!C && a + b + c != 1)))
					{
						System.out.println(A?"诚实族" : "说谎族");
						System.out.println(B?"诚实族" : "说谎族");
						System.out.println(C?"诚实族" : "说谎族");						
					}
				}
			}
		}
	}

}

Output:

说谎族
说谎族
说谎族
Released eight original articles · won praise 6 · views 178

Guess you like

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