一個好玩的算法題。用java來寫寫

public class test {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 0代表身份,0代表誠實族,1代表撒謊族,2代表兩面族。
int[] answerArr = new int[] { 2, 0, 1 };
/**
* answerArr對應三個人的答案,[中間的人。左邊的人,右邊的人] 由於答案都是圍繞中間的人。所以把中間的人放在0位
*/
/**
* for循環中i代表了假設的中間的人的真實的身份
*/
int a;// 中間的人
int b;// 左邊的人
int c;// 右邊的人
/**
* 枚舉所有情況
*/
for (int i = 0; i < 3; i++) {
a = i;
b = (i + 1) % 3;
c = (i + 2) % 3;
System.out.print(a + "," + b + "," + c);
System.out.println(":" + check(a, a == 2) + "," + check(b, a == 0)
+ "," + check(c, a == 1));
b = (i + 2) % 3;
c = (i + 1) % 3;
System.out.print(a + "," + b + "," + c);
System.out.println(":" + check(a, a == 2) + "," + check(b, a == 0)
+ "," + check(c, a == 1));
}
}


/**
* i是被檢查人的身份。 answer是代表按照當前的身份他說的是真話還是假話

* @param i
* @param answer
* @return
*/
public static boolean check(int i, boolean answer) {
if (i == 0)
return answer == true ? true : false;
if (i == 1)
return answer == false ? true : false;
if (i == 2)
return true;
return false;


}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章