谁养鱼


/**
*
* @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
鱼 猫 狗 马 鸟
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值