文章詳情頁
愛因斯坦謎題的java解答方法
瀏覽:83日期:2024-06-13 16:02:07
內(nèi)容: 愛因斯坦的推理題:1.有5棟5種顏色的房子2.每一位房子的主人國籍都不同3.這五個人每人只喝一個牌子的飲料,只抽一個牌子的香煙,只養(yǎng)一種寵物4.沒有人有相同的寵物,抽相同牌子的煙,喝相同牌子的飲料已知條件:1.英國人住在紅房子里2.瑞典人養(yǎng)了一條狗3.丹麥人喝茶4.綠房子在白房子的左邊5.綠房子主人喝咖啡6.抽pallmall煙的人養(yǎng)了一只鳥7.黃房子主人抽dunhill煙8.住在中間房子的人喝牛奶9.挪威人住在第一間房子10.抽混合煙的人住在養(yǎng)貓人的旁邊11.養(yǎng)馬人住在抽dunhill煙人的旁邊12.抽bluemaster煙的人喝啤酒13.德國人抽prince煙14.挪威人住在藍(lán)房子旁邊15.抽混合煙的人的鄰居喝礦泉水問題:誰養(yǎng)魚?========================================Start========================================答案1挪威人 丹麥人 英國人 德國人 瑞典人 黃房子 藍(lán)房子 紅房子 綠房子 白房子 貓 馬 鳥 魚 狗 水 茶 牛奶 咖啡 啤酒 DUNHILL 混合煙 PALLMALL PRINCE BLUEMASTER -------------------------------------------------------------------------------------答案2挪威人 德國人 英國人 丹麥人 瑞典人 綠房子 藍(lán)房子 紅房子 黃房子 白房子 鳥 貓 馬 魚 狗 咖啡 水 牛奶 茶 啤酒 PALLMALL PRINCE 混合煙 DUNHILL BLUEMASTER -------------------------------------------------------------------------------------答案3挪威人 德國人 英國人 丹麥人 瑞典人 綠房子 藍(lán)房子 紅房子 黃房子 白房子 鳥 魚 馬 貓 狗 咖啡 水 牛奶 茶 啤酒 PALLMALL PRINCE 混合煙 DUNHILL BLUEMASTER -------------------------------------------------------------------------------------答案4挪威人 德國人 瑞典人 英國人 丹麥人 綠房子 藍(lán)房子 黃房子 紅房子 白房子 魚 貓 狗 馬 鳥 咖啡 水 牛奶 啤酒 茶 混合煙 PRINCE DUNHILL BLUEMASTER PALLMALL -------------------------------------------------------------------------------------答案5挪威人 德國人 瑞典人 英國人 丹麥人 綠房子 藍(lán)房子 白房子 紅房子 黃房子 鳥 貓 狗 馬 魚 咖啡 水 牛奶 啤酒 茶 PALLMALL PRINCE 混合煙 BLUEMASTER DUNHILL -------------------------------------------------------------------------------------答案6挪威人 德國人 瑞典人 丹麥人 英國人 綠房子 藍(lán)房子 白房子 黃房子 紅房子 鳥 貓 狗 魚 馬 咖啡 水 牛奶 茶 啤酒 PALLMALL PRINCE 混合煙 DUNHILL BLUEMASTER -------------------------------------------------------------------------------------答案7挪威人 德國人 瑞典人 丹麥人 英國人 綠房子 藍(lán)房子 白房子 黃房子 紅房子 鳥 魚 狗 貓 馬 咖啡 水 牛奶 茶 啤酒 PALLMALL PRINCE 混合煙 DUNHILL BLUEMASTER -------------------------------------------------------------------------------------========================================End==========================================java代碼:/* * Created on 2005-11-8 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package com.netengine.test;/** * @author cds * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class WhoFeedsFish { private static final String problem = '愛因斯坦的推理題:nn'+ '1.有5棟5種顏色的房子n'+ '2.每一位房子的主人國籍都不同n'+ '3.這五個人每人只喝一個牌子的飲料,只抽一個牌子的香煙,只養(yǎng)一種寵物n'+ '4.沒有人有相同的寵物,抽相同牌子的煙,喝相同牌子的飲料nn'+ '已知條件:n'+ '1.英國人住在紅房子里n'+ '2.瑞典人養(yǎng)了一條狗n'+ '3.丹麥人喝茶n'+ '4.綠房子在白房子的左邊n'+ '5.綠房子主人喝咖啡n'+ '6.抽pallmall煙的人養(yǎng)了一只鳥n'+ '7.黃房子主人抽dunhill煙n'+ '8.住在中間房子的人喝牛奶n'+ '9.挪威人住在第一間房子n'+ '10.抽混合煙的人住在養(yǎng)貓人的旁邊n'+ '11.養(yǎng)馬人住在抽dunhill煙人的旁邊n'+ '12.抽bluemaster煙的人喝啤酒n'+ '13.德國人抽prince煙n'+ '14.挪威人住在藍(lán)房子旁邊n'+ '15.抽混合煙的人的鄰居喝礦泉水nn'+ '問題:誰養(yǎng)魚?n'; /** * @return Returns the problem. */ public String getProblem() { return problem; } private static final int NATIONALITY_ENGLISH = 1; private static final int NATIONALITY_SWIDISH = 2; private static final int NATIONALITY_DAMARK = 3; private static final int NATIONALITY_NORWAY = 4; private static final int NATIONALITY_GERMAN = 5; private int[] nationalities = new int[5]; private static final int COLOR_RED = 1; private static final int COLOR_GREEN = 2; private static final int COLOR_YELLOW = 3; private static final int COLOR_WHITE = 4; private static final int COLOR_BLUE = 5; private int[] colors = new int[5]; private static final int PET_DOG = 1; private static final int PET_BIRD = 2; private static final int PET_CAT = 3; private static final int PET_HORSE = 4; private static final int PET_FISH = 5; private int[] pets = new int[5]; private static final int DRINK_TEA = 1; private static final int DRINK_COFFEE = 2; private static final int DRINK_MILK = 3; private static final int DRINK_BEER = 4; private static final int DRINK_WATER = 5; private int[] drinks = new int[5]; private static final int TOBACCO_PALLMALL = 1; private static final int TOBACCO_DUNHILL = 2; private static final int TOBACCO_BLUEMASTER = 3; private static final int TOBACCO_PRINCE = 4; private static final int TOBACCO_MIXED = 5; private int[] tobaccoes = new int[5]; //5*5的二維數(shù)組,答案就在其中: private int[][] key ={nationalities,colors,pets,drinks,tobaccoes}; private static final int[][] values = { {1,2,3,4,5}, {1,2,3,5,4}, {1,2,4,3,5}, {1,2,4,5,3}, {1,2,5,3,4}, {1,2,5,4,3}, {1,3,2,4,5}, {1,3,2,5,4}, {1,3,4,2,5}, {1,3,4,5,2}, {1,3,5,2,4}, {1,3,5,4,2}, {1,4,2,3,5}, {1,4,2,5,3}, {1,4,3,2,5}, {1,4,3,5,2}, {1,4,5,2,3}, {1,4,5,3,2}, {1,5,2,3,4}, {1,5,2,4,3}, {1,5,3,2,4}, {1,5,3,4,2}, {1,5,4,2,3}, {1,5,4,3,2}, {2,1,3,4,5}, {2,1,3,5,4}, {2,1,4,3,5}, {2,1,4,5,3}, {2,1,5,3,4}, {2,1,5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}, {5,4,1,3,2}, {5,4,2,1,3}, {5,4,2,3,1}, {5,4,3,1,2}, {5,4,3,2,1} }; public void printKey(){ for(int i = 0; i < 5; i ++){ print('nationality',key[0][i]); } System.out.println(); for(int i = 0; i < 5; i ++){ print('color',key[1][i]); } System.out.println(); for(int i = 0; i < 5; i ++){ print('pet',key[2][i]); } System.out.println(); for(int i = 0; i < 5; i ++){ print('drink',key[3][i]); } System.out.println(); for(int i = 0; i < 5; i ++){ print('tobacco',key[4][i]); } System.out.println(); } private void print(String item,int index){ if(false){ }else if('nationality'.equals(item)){ switch(index){ case 1: System.out.print('英國人t'); break; case 2: System.out.print('瑞典人t'); break; case 3: System.out.print('丹麥人t'); break; case 4: System.out.print('挪威人t'); break; case 5: System.out.print('德國人t'); break; } }else if('color'.equals(item)){ switch(index){ case 1: System.out.print('紅房子t'); break; case 2: System.out.print('綠房子t'); break; case 3: System.out.print('黃房子t'); break; case 4: System.out.print('白房子t'); break; case 5: System.out.print('藍(lán)房子t'); break; } }else if('pet'.equals(item)){ switch(index){ case 1: System.out.print('狗t'); break; case 2: System.out.print('鳥t'); break; case 3: System.out.print('貓t'); break; case 4: System.out.print('馬t'); break; case 5: System.out.print('魚t'); break; } }else if('drink'.equals(item)){ switch(index){ case 1: System.out.print('茶t'); break; case 2: System.out.print('咖啡t'); break; case 3: System.out.print('牛奶t'); break; case 4: System.out.print('啤酒t'); break; case 5: System.out.print('水t'); break; } }else if('tobacco'.equals(item)){ switch(index){ case 1: System.out.print('PALLMALLt'); break; case 2: System.out.print('DUNHILLt'); break; case 3: System.out.print('BLUEMASTERt'); break; case 4: System.out.print('PRINCEt'); break; case 5: System.out.print('混合煙t'); break; } } }// 條件1:英國人住在紅房子里 01 private boolean check01(){ for(int i = 0; i < nationalities.length; i ++){ if(key[0][i] == NATIONALITY_ENGLISH){if(key[1][i] != COLOR_RED){return false;}else{return true;} } } return false; } // 條件2:瑞典人養(yǎng)了一條狗 02 private boolean check02(){ for(int i = 0; i < nationalities.length; i ++){ if(key[0][i] == NATIONALITY_SWIDISH){if(key[2][i] != PET_DOG){return false;}else{return true;} } } return false; }// 條件4:綠房子在白房子的左邊 1 private boolean check1(){ for(int i = 0; i < colors.length; i ++){ if(key[1][i] == COLOR_GREEN){for(int j = 0; j < colors.length; j ++){if(key[1][j] == COLOR_WHITE){ if(i> j){ return false; } else{ return true; }}} } } return false; } // 條件8:住在中間房子的人喝牛奶 3 private boolean check3(){ return key[3][2] == DRINK_MILK?true:false; } // 條件9:挪威人住在第一間房子 0 private boolean check0(){ if(key[0][0] != NATIONALITY_NORWAY){ return false; } return true; } // 14.挪威人住在藍(lán)房子旁邊 01 private boolean check011(){ for(int i = 0; i < nationalities.length; i ++){ if(key[0][i] == NATIONALITY_NORWAY){for(int j = 0; j < colors.length; j ++){if(key[1][j] == COLOR_BLUE){ if(Math.abs(i - j) == 1){ return true; }else{ return false; }}} } }return false; } /////////////////////////////////////////////////////////////////// //條件1,2,3,4,8,9,14被拿出來提前檢測(預(yù)檢測以去除不必要的操作,減少執(zhí)行時間) private boolean check(){// 條件3:丹麥人喝茶 03 for(int i = 0; i < nationalities.length; i ++){ if(key[0][i] == NATIONALITY_DAMARK){if(key[3][i] != DRINK_TEA){return false;}else{break;} } }// 條件5:綠房子主人喝咖啡 13 for(int i = 0; i < colors.length; i ++){ if(key[1][i] == COLOR_GREEN){if(key[3][i] != DRINK_COFFEE){return false;}else{break;} } } // 條件6:抽pallmall煙的人養(yǎng)了一只鳥 24 for(int i = 0; i < tobaccoes.length; i ++){ if(key[4][i] == TOBACCO_PALLMALL){if(key[2][i] != PET_BIRD){return false;}else{break;} } } // 條件7:黃房子主人抽dunhill煙 14 for(int i = 0; i < colors.length; i ++){ if(key[1][i] == COLOR_YELLOW){if(key[4][i] != TOBACCO_DUNHILL){return false;}else{break;} } }// 條件10:抽混合煙的人住在養(yǎng)貓人的旁邊 24 for(int i = 0; i < tobaccoes.length; i ++){ if(key[4][i] == TOBACCO_MIXED){for(int j = 0; j < pets.length; j ++){if(key[2][j] == PET_CAT){ if(i - j != 1 && i - j != -1){ return false; } break;}}break; } } // 條件11:養(yǎng)馬人住在抽dunhill煙人的旁邊 24 for(int i = 0; i < pets.length; i ++){ if(key[2][i] == PET_HORSE){for(int j = 0; j < tobaccoes.length; j ++){if(key[4][j] == TOBACCO_DUNHILL){ if(i - j != 1 && i - j != -1){ return false; } break;}}break; } } // 條件12:抽bluemaster煙的人喝啤酒 34 for(int i = 0; i < tobaccoes.length; i ++){ if(key[4][i] == TOBACCO_BLUEMASTER){if(key[3][i] != DRINK_BEER){return false;}else{break;} } } // 13.德國人抽prince煙 04 for(int i = 0; i < nationalities.length; i ++){ if(key[0][i] == NATIONALITY_GERMAN){if(key[4][i] != TOBACCO_PRINCE){return false;}else{break;} } }// 15.抽混合煙的人的鄰居喝礦泉水 34 for(int i = 0; i < tobaccoes.length; i ++){ if(key[4][i] == TOBACCO_MIXED){for(int j = 0; j < drinks.length; j ++){if(key[3][j] == DRINK_WATER){ if((i - j != 1) && (i - j != -1)){ return false; } else{ break; }}}break; } } //滿足所有條件: return true; } public void run(){ int count = 0; for(int a = 0; a < 120; a ++){ for(int i = 0; i < 5; i ++){key[0][i] = values[a][i]; } if(!check0()){continue;} for(int b = 0; b < 120; b ++){for(int i = 0; i < 5; i ++){key[1][i] = values[b][i];}if(!check01() || !check011() || !check1()){continue;}for(int c = 0; c < 120; c ++){for(int i = 0; i < 5; i ++){ key[2][i] = values[c][i];}if(!check02()){continue;}for(int d = 0; d < 120; d ++){ for(int i = 0; i < 5; i ++){ key[3][i] = values[d][i]; } if(!check3()){continue;} for(int e = 0; e < 120; e ++){ for(int i = 0; i < 5; i ++){ key[4][i] = values[e][i]; } if(!check()){continue;} System.out.println('答案' + (++count)); printKey(); System.out.println('-----------------------------------------------'+'--------------------------------------'); } }} } } } public static void main(String[] args){ WhoFeedsFish wff = new WhoFeedsFish(); System.out.println(wff.getProblem()); System.out.println('========================================Start========================================'); wff.run(); System.out.println('========================================End=========================================='); }} Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 愛因斯坦的推理題:1.有5棟5種顏色的房子2.每一位房子的主人國籍都不同
標(biāo)簽:
Java
相關(guān)文章:
1. 微信小程序視圖層莫名出現(xiàn)豎線的解決方法2. 基于Surprise協(xié)同過濾實(shí)現(xiàn)短視頻推薦方法示例3. .NET SkiaSharp 生成二維碼驗證碼及指定區(qū)域截取方法實(shí)現(xiàn)4. IIS+PHP添加對webp格式圖像的支持配置方法5. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法6. Spring注入Date類型的三種方法總結(jié)7. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解8. 一文掌握在Vue3中書寫TSX的使用方法9. numba提升python運(yùn)行速度的實(shí)例方法10. python 實(shí)現(xiàn)在無序數(shù)組中找到中位數(shù)方法
排行榜
