誰養魚


/**
*
* @author jia.hej
*
* 愛因斯坦推理題:誰養魚
*
* 1、在一條街上,有5座房子,噴了5種顏色。
*
* 2、每個房裏住着不同國籍的人
*
* 3、每個人喝不同的飲料,抽不同品牌的香菸,養不同的寵物
*
* 問題是:誰養魚?
*
* 提示:
*
* 1、英國人住紅色房子
*
* 2、瑞典人養狗
*
* 3、丹麥人喝茶
*
* 4、綠色房子在白色房子左面
*
* 5、綠色房子主人喝咖啡
*
* 6、抽PallMall香菸的人養鳥
*
* 7、黃色房子主人抽Dunhill香菸
*
* 8、住在中間房子的人喝牛奶
*
* 9、 挪威人住第一間房
*
* 10、抽Blends香菸的人住在養貓的人隔壁
*
* 11、養馬的人住在抽Dunhill香菸的人隔壁
*
* 12、抽BlueMaster的人喝啤酒
*
* 13、德國人抽Prince香菸
*
* 14、挪威人住藍色房子隔壁
*
* 15、抽Blends香菸的人有一個喝水的鄰居
*
*
* 愛因斯坦在20世紀初出的這個謎語。他說世界上有98%的人答不出來。
*
*/
public class Fish {

private static int count = 0;

//----------------------初始化---------------------//
/** 國籍 */
private static final String[] COUNTRY = { "丹麥", "英國", "德國", "挪威", "瑞典" };
/** 房間 */
private static final String[] ROOM = { "藍色", "綠色", "紅色", "白色", "黃色" };
/** 飲料 */
private static final String[] DRINK = { "啤酒", "咖啡", "牛奶", "茶", "水" };
/** 煙 */
private static final String[] SMOKE = { "BLENDS", "BLUEMASTER", "DUNHILL", "PALLMALL",
"PRINCE" };
/** 寵物 */
private static final String[] PET = { "鳥", "貓", "狗", "魚", "馬" };

/** 國籍 */
private static int[] COUNTRY_INDEX = { 0, 1, 2, 3, 4 };
/** 房間 */
private static int[] ROOM_INDEX = { 0, 1, 2, 3, 4 };
/** 飲料 */
private static int[] DRINK_INDEX = { 0, 1, 2, 3, 4 };
/** 煙 */
private static int[] SMOKE_INDEX = { 0, 1, 2, 3, 4 };
/** 寵物 */
private static int[] PET_INDEX = { 0, 1, 2, 3, 4 };

//----------------------全排列---------------------//
private static final int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 0 }, { 4, 2, 3, 0, 1 }, { 4, 2, 3, 1, 0 },
{ 4, 3, 0, 1, 2 }, { 4, 3, 0, 2, 1 }, { 4, 3, 1, 0, 2 }, { 4, 3, 1, 2, 0 },
{ 4, 3, 2, 0, 1 }, { 4, 3, 2, 1, 0 } };

public static void main(String[] args) {
//a、遍歷COUNTRY
for (int a = 0; a < 120; a++) {
for (int b = 0; b < 5; b++) {
COUNTRY_INDEX[b] = INDEX[a][b];
}
if (!checkCondition9()) {
continue;
}
//b、遍歷ROOM
for (int c = 0; c < 120; c++) {
for (int d = 0; d < 5; d++) {
ROOM_INDEX[d] = INDEX[c][d];
}
if (!checkCondition1() || !checkCondition4() || !checkCondition14()) {
continue;
}
//c、遍歷DRINK
for (int e = 0; e < 120; e++) {
for (int f = 0; f < 5; f++) {
DRINK_INDEX[f] = INDEX[e][f];
}
if (!checkCondition3() || !checkCondition5() || !checkCondition8()) {
continue;
}
//d、遍歷SMOKE
for (int g = 0; g < 120; g++) {
for (int h = 0; h < 5; h++) {
SMOKE_INDEX[h] = INDEX[g][h];
}
if (!checkCondition7() || !checkCondition12() || !checkCondition13()) {
continue;
}
//e、遍歷PET
for (int i = 0; i < 120; i++) {
for (int j = 0; j < 5; j++) {
PET_INDEX[j] = INDEX[i][j];
}
if (!checkCondition2() || !checkCondition6() || !checkCondition10()
|| !checkCondition11() || !checkCondition15()) {
continue;
}

//output
System.out.println("--------------" + (++count) + "---------------");
for (int z = 0; z < COUNTRY_INDEX.length; z++) {
System.out.print(COUNTRY[COUNTRY_INDEX[z]] + "\t\t");
}
System.out.println();
for (int y = 0; y < ROOM_INDEX.length; y++) {
System.out.print(ROOM[ROOM_INDEX[y]] + "\t\t");
}
System.out.println();
for (int x = 0; x < DRINK_INDEX.length; x++) {
System.out.print(DRINK[DRINK_INDEX[x]] + "\t\t");
}
System.out.println();
for (int w = 0; w < SMOKE_INDEX.length; w++) {
System.out.print(SMOKE[SMOKE_INDEX[w]] + "\t\t");
}
System.out.println();
for (int v = 0; v < PET_INDEX.length; v++) {
System.out.print(PET[PET_INDEX[v]] + "\t\t");
}
System.out.println();
}
}
}
}
}
}

//1、英國人住紅色房子
private static boolean checkCondition1() {
for (int i = 0; i < COUNTRY_INDEX.length; i++) {
for (int j = 0; j < ROOM_INDEX.length; j++) {
if (COUNTRY[COUNTRY_INDEX[i]].equals("英國") && ROOM[ROOM_INDEX[j]].equals("紅色")
&& i == j) {
return true;
}
}
}

return false;
}

//2、瑞典人養狗
private static boolean checkCondition2() {
for (int i = 0; i < COUNTRY_INDEX.length; i++) {
for (int j = 0; j < PET_INDEX.length; j++) {
if (COUNTRY[COUNTRY_INDEX[i]].equals("瑞典") && PET[PET_INDEX[j]].equals("狗")
&& i == j) {
return true;
}
}
}
return false;
}

//3、丹麥人喝茶
private static boolean checkCondition3() {
for (int i = 0; i < COUNTRY_INDEX.length; i++) {
for (int j = 0; j < DRINK_INDEX.length; j++) {
if (COUNTRY[COUNTRY_INDEX[i]].equals("丹麥") && DRINK[DRINK_INDEX[j]].equals("茶")
&& i == j) {
return true;
}
}
}
return false;
}

//4、綠色房子在白色房子左面
private static boolean checkCondition4() {
for (int i = 0; i < ROOM_INDEX.length; i++) {
for (int j = 0; j < ROOM_INDEX.length; j++) {
if (ROOM[ROOM_INDEX[i]].equals("綠色") && ROOM[ROOM_INDEX[j]].equals("白色") && i < j) {
return true;
}
}
}
return false;
}

//5、綠色房子主人喝咖啡
private static boolean checkCondition5() {
for (int i = 0; i < ROOM_INDEX.length; i++) {
for (int j = 0; j < DRINK_INDEX.length; j++) {
if (ROOM[ROOM_INDEX[i]].equals("綠色") && DRINK[DRINK_INDEX[j]].equals("咖啡")
&& i == j) {
return true;
}
}
}
return false;
}

//6、抽PallMall香菸的人養鳥
private static boolean checkCondition6() {
for (int i = 0; i < SMOKE_INDEX.length; i++) {
for (int j = 0; j < PET_INDEX.length; j++) {
if (SMOKE[SMOKE_INDEX[i]].equals("PALLMALL") && PET[PET_INDEX[j]].equals("鳥")
&& i == j) {
return true;
}
}
}
return false;
}

//7、黃色房子主人抽Dunhill香菸
private static boolean checkCondition7() {
for (int i = 0; i < ROOM_INDEX.length; i++) {
for (int j = 0; j < SMOKE_INDEX.length; j++) {
if (ROOM[ROOM_INDEX[i]].equals("黃色") && SMOKE[SMOKE_INDEX[j]].equals("DUNHILL")
&& i == j) {
return true;
}
}
}
return false;
}

//8、住在中間房子的人喝牛奶
private static boolean checkCondition8() {
if (DRINK[DRINK_INDEX[2]].equals("牛奶")) {
return true;
}
return false;
}

//9、挪威人住第一間房
private static boolean checkCondition9() {
if (COUNTRY[COUNTRY_INDEX[0]].equals("挪威")) {
return true;
}
return false;
}

//10、抽Blends香菸的人住在養貓的人隔壁
private static boolean checkCondition10() {
for (int i = 0; i < SMOKE_INDEX.length; i++) {
for (int j = 0; j < PET_INDEX.length; j++) {
if (SMOKE[SMOKE_INDEX[i]].equals("BLENDS") && PET[PET_INDEX[j]].equals("貓")
&& (j - i == 1 || j - i == -1)) {
return true;
}
}
}
return false;
}

//11、養馬的人住在抽Dunhill香菸的人隔壁
private static boolean checkCondition11() {
for (int i = 0; i < PET_INDEX.length; i++) {
for (int j = 0; j < SMOKE_INDEX.length; j++) {
if (PET[PET_INDEX[i]].equals("馬") && SMOKE[SMOKE_INDEX[j]].equals("DUNHILL")
&& (j - i == 1 || j - i == -1)) {
return true;
}
}
}
return false;
}

//12、抽BlueMaster的人喝啤酒
private static boolean checkCondition12() {
for (int i = 0; i < SMOKE_INDEX.length; i++) {
for (int j = 0; j < DRINK_INDEX.length; j++) {
if (SMOKE[SMOKE_INDEX[i]].equals("BLUEMASTER")
&& DRINK[DRINK_INDEX[j]].equals("啤酒") && i == j) {
return true;
}
}
}
return false;
}

//13、德國人抽Prince香菸
private static boolean checkCondition13() {
for (int i = 0; i < COUNTRY_INDEX.length; i++) {
for (int j = 0; j < SMOKE_INDEX.length; j++) {
if (COUNTRY[COUNTRY_INDEX[i]].equals("德國")
&& SMOKE[SMOKE_INDEX[j]].equals("PRINCE") && i == j) {
return true;
}
}
}
return false;
}

//14、挪威人住藍色房子隔壁
private static boolean checkCondition14() {
for (int i = 0; i < COUNTRY_INDEX.length; i++) {
for (int j = 0; j < ROOM_INDEX.length; j++) {
if (COUNTRY[COUNTRY_INDEX[i]].equals("挪威") && ROOM[ROOM_INDEX[j]].equals("藍色")
&& (j - i == 1 || j - i == -1)) {
return true;
}
}
}
return false;
}

//15、抽Blends香菸的人有一個喝水的鄰居
private static boolean checkCondition15() {
for (int i = 0; i < SMOKE_INDEX.length; i++) {
for (int j = 0; j < DRINK_INDEX.length; j++) {
if (SMOKE[SMOKE_INDEX[i]].equals("BLENDS") && DRINK[DRINK_INDEX[j]].equals("水")
&& (i - j == 1 || i - j == -1)) {
return true;
}
}
}
return false;
}

// public static void main(String[] args) {
// int[] indexArray = { 0, 1, 2, 3, 4 };
//
// do {
// System.out.print("{");
// for (int i = 0; i < 5; i++) {
// System.out.print(indexArray[i]);
// if (i != 4) {
// System.out.print(",");
// }
// }
// System.out.print("},");
// System.out.println();
// } while (nextPermutation(indexArray));
// }
//
// private static boolean nextPermutation(int[] arr) {
// int postLeft = -1;
// for (int i = arr.length - 1; i > 0; i--) {
// if (arr[i - 1] < arr[i]) {
// postLeft = i - 1;
// break;
// }
// }
// if (postLeft < 0) {
// return false;
// }
//
// int postRight = -1;
// for (int i = arr.length - 1; i >= postLeft; i--) {
// if (arr[i] > arr[postLeft]) {
// postRight = i;
// break;
// }
// }
// swap(arr, postLeft, postRight);
// reverse(arr, postLeft + 1, arr.length);
// return true;
// }
//
// private static void swap(int[] arr, int ind1, int ind2) {
// int t = arr[ind1];
// arr[ind1] = arr[ind2];
// arr[ind2] = t;
// }
//
// private static void reverse(int[] arr, int ind1, int ind2) {
// for (int i = 0; i < (ind2 - ind1) / 2; i++) {
// swap(arr, ind1 + i, ind2 - 1 - (i));
// }
// }
}

--------------1---------------
挪威 丹麥 英國 德國 瑞典
黃色 藍色 紅色 綠色 白色
水 茶 牛奶 咖啡 啤酒
DUNHILL BLENDS PALLMALL PRINCE BLUEMASTER
貓 馬 鳥 魚 狗
--------------2---------------
挪威 德國 英國 丹麥 瑞典
綠色 藍色 紅色 黃色 白色
咖啡 水 牛奶 茶 啤酒
PALLMALL PRINCE BLENDS DUNHILL BLUEMASTER
鳥 貓 馬 魚 狗
--------------3---------------
挪威 德國 英國 丹麥 瑞典
綠色 藍色 紅色 黃色 白色
咖啡 水 牛奶 茶 啤酒
PALLMALL PRINCE BLENDS DUNHILL BLUEMASTER
鳥 魚 馬 貓 狗
--------------4---------------
挪威 德國 瑞典 丹麥 英國
綠色 藍色 白色 黃色 紅色
咖啡 水 牛奶 茶 啤酒
PALLMALL PRINCE BLENDS DUNHILL BLUEMASTER
鳥 貓 狗 魚 馬
--------------5---------------
挪威 德國 瑞典 丹麥 英國
綠色 藍色 白色 黃色 紅色
咖啡 水 牛奶 茶 啤酒
PALLMALL PRINCE BLENDS DUNHILL BLUEMASTER
鳥 魚 狗 貓 馬
--------------6---------------
挪威 德國 瑞典 英國 丹麥
綠色 藍色 白色 紅色 黃色
咖啡 水 牛奶 啤酒 茶
PALLMALL PRINCE BLENDS BLUEMASTER DUNHILL
鳥 貓 狗 馬 魚
--------------7---------------
挪威 德國 瑞典 英國 丹麥
綠色 藍色 黃色 紅色 白色
咖啡 水 牛奶 啤酒 茶
BLENDS PRINCE DUNHILL BLUEMASTER PALLMALL
魚 貓 狗 馬 鳥
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章