资源预览内容
第1页 / 共45页
第2页 / 共45页
第3页 / 共45页
第4页 / 共45页
第5页 / 共45页
第6页 / 共45页
第7页 / 共45页
第8页 / 共45页
第9页 / 共45页
第10页 / 共45页
亲,该文档总共45页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
爱因斯坦谜题: 在一条街上有颜色互不相同的五栋房子,不同国籍的人分别住在这五栋房子力,每人抽不同品牌的香烟,喝不同的饮料,养不同的宠物。已知如下情况:1. 英国人住红色房子里。2. 瑞典人养狗。3. 丹麦人喝茶。4. 绿色房子坐落在白色房子的左面。5. 绿色房子的主人喝咖啡。6. 抽Pall Mall香烟的人养鸟。7. 黄色房子的主人抽Dunhill香烟。8. 挪威人住第一间房子。9. 五座房子中间的那座的主人喝牛奶。10. 抽Blends香烟的住在养猫人的隔壁。11. 养马的人住在抽Dunhill香烟者的隔壁。12. 抽Blue Master香烟的喝啤酒。13. 德国人抽Prince香烟。14. 挪威人住的房子在蓝色房子的隔壁。15. 抽Blends香烟的人有一个喝水的邻居。问:谁养鱼?谜题的英文原文:Let us assume that there are five houses of different colors next to each other on the same road. In each house lives a man of a different nationality. Every man has his favorite drink, his favorite brand of cigarettes, and keeps pets of a particular kind. 1.The Englishman lives in the red house. 2.The Swede keeps dogs. 3.The Dane drinks tea. 4.The green house is just to the left of the white one. 5.The owner of the green house drinks coffee. 6.The Pall Mall smoker keeps birds. 7.The owner of the yellow house smokes Dunhills. 8.The man in the center house drinks milk. 9.The Norwegian lives in the first house. 10.The Blend smoker has a neighbor who keeps cats. 11.The man who smokes Blue Masters drinks bier. 12.The man who keeps horses lives next to the Dunhill smoker. 13.The German smokes Prince. 14.The Norwegian lives next to the blue house. 15.The Blend smoker has a neighbor who drinks water. The question to be answered is: Who keeps fish?这道迷题出自1981年柏林的德国逻辑思考学院。据说世界上只有2%的人能出答案。就连大名鼎鼎的爱因斯坦也成为此题大伤脑筋,所以这道题也经常被国内外知名公司用做面试题目,相信许多朋友都只做出过一个答案,今天就用计算机来看看答案:C#代码(代码来源:http:/tech.ccidnet.com/art/322/20040304/94061_1.html,在此感谢原作者):usingSystem;namespacenetsafe.mathpublicclassayst/问题中的所有元素/string,data=黄房子,蓝房子,白房子,红房子,绿房子,挪威人,英国人,德国人,丹麦人,瑞典人,DUNHILL,PRINCE,混合烟,PALLMALL,BLUEMASTER,咖啡,矿泉水,茶,牛奶,啤酒,鱼,恐龙,马,鸟,狗;/answer用来存放答案/int,answer=newint6,6;int,ALL=newint6,122;intcount=1;intnLevel=0;intList=newint6;publicstaticvoidMain(stringargs)aystc=newayst();c.p();/生成全排列到allc.run();Console.Read();/按任意键继续voidrun()inti1,i2,i3,i4,i5;/通过逻辑条件顺序的有效选择来优化程序for(i1=1;i1=120;i1+)/房子/9、挪威人住第一间房子/14、挪威人住在蓝房子旁边/不满足条件就短路/if(ALL2,i1!=2)continue;for(intj=0;j5;j+,answerj,1=ALLj,i1);for(i2=1;i2=120;i2+)/人种for(intj=0;jfind(1,3)continue;for(i3=1;i3=120;i3+)/烟for(intj=0;j5;j+,answerj,3=ALLj,i3);/13、德国人抽PRINCE烟/if(find(2,3)!=find(3,2)continue;/7、黄房子主人抽DUNHILL烟/if(find(1,1)!=find(3,1)continue;for(i4=1;i4=120;i4+)/饮料for(intj=0;j5;j+,answerj,4=ALLj,i4);/8、住在中间那间房子的人喝牛奶/if(ALL3,i4!=4)continue;/5、绿房子主人喝咖啡/if(find(1,5)!=find(4,1)continue;/3、丹麦人喝茶/if(find(2,4)!=find(4,3)continue;/15、抽混合烟的人的邻居喝矿泉水if(Math.Abs(find(3,3)-find(4,2)!=1)continue;/12、抽BLUEMASTER烟的人喝啤酒/if(find(3,5)!=find(4,5)continue;for(i5=1;i5=120;i5+)/宠物for(intj=0;j5;j+,answerj,5=ALLj,i5);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号