爱因斯坦的谜题

爱因斯坦的谜题:
    在一条街上有颜色互不相同的五栋房子,不同国籍的人分别住在这五栋房子力,每人抽不同品牌的香烟,喝不同的饮料,养不同的宠物。已知如下情况:
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%的人能出答案。就连大名鼎鼎的爱因斯坦也成为此题大伤脑筋,所以这道题也经常被国内外知名公司用做面试题目,相信许多朋友都只做出过一个答案,今天就用计算机来看看答案:

 

000001        package my.code.reflectTest;
000002       
000003       
000009        public class WhoFeedsFish {
000010       
000011        private static final String problem = "爱因斯坦的推理题:\n\n" + "1.有5栋5种颜色的房子\n"
000012                + "2.每一位房子的主人国籍都不同\n" + "3.这五个人每人只喝一个牌子的饮料,只抽一个牌子的香烟,只养一种宠物\n"
000013                + "4.没有人有相同的宠物,抽相同牌子的烟,喝相同牌子的饮料\n\n" + "已知条件:\n" + "1.英国人住在红房子里\n"
000014                + "2.瑞典人养了一条狗\n" + "3.丹麦人喝茶\n" + "4.绿房子在白房子的左边\n" + "5.绿房子主人喝咖啡\n"
000015                + "6.抽pallmall烟的人养了一只鸟\n" + "7.黄房子主人抽dunhill烟\n"
000016                + "8.住在中间房子的人喝牛奶\n" + "9.挪威人住在第一间房子\n"
000017                + "10.抽Blue Master的人住在养猫人的旁边\n" + "11.养马人住在抽dunhill烟人的旁边\n"
000018                + "12.抽bluemaster烟的人喝啤酒\n" + "13.德国人抽prince烟\n" + "14.挪威人住在蓝房子旁边\n"
000019                + "15.抽Blue Master的人的邻居喝矿泉水\n\n" + "问题:谁养鱼?\n";
000020       
000021       
000024        public String getProblem() {
000025                return problem;
000026        }
000027       
000028        private static final int NATIONALITY_ENGLISH = 1;
000029        private static final int NATIONALITY_SWIDISH = 2;
000030        private static final int NATIONALITY_DAMARK = 3;
000031        private static final int NATIONALITY_NORWAY = 4;
000032        private static final int NATIONALITY_GERMAN = 5;
000033        private int[] nationalities = new int[5];
000034       
000035        private static final int COLOR_RED = 1;
000036        private static final int COLOR_GREEN = 2;
000037        private static final int COLOR_YELLOW = 3;
000038        private static final int COLOR_WHITE = 4;
000039        private static final int COLOR_BLUE = 5;
000040        private int[] colors = new int[5];
000041       
000042        private static final int PET_DOG = 1;
000043        private static final int PET_BIRD = 2;
000044        private static final int PET_CAT = 3;
000045        private static final int PET_HORSE = 4;
000046        private static final int PET_FISH = 5;
000047        private int[] pets = new int[5];
000048       
000049        private static final int DRINK_TEA = 1;
000050        private static final int DRINK_COFFEE = 2;
000051        private static final int DRINK_MILK = 3;
000052        private static final int DRINK_BEER = 4;
000053        private static final int DRINK_WATER = 5;
000054        private int[] drinks = new int[5];
000055       
000056        private static final int TOBACCO_PALLMALL = 1;
000057        private static final int TOBACCO_DUNHILL = 2;
000058        private static final int TOBACCO_BLUEMASTER = 3;
000059        private static final int TOBACCO_PRINCE = 4;
000060        private static final int TOBACCO_MIXED = 5;
000061        private int[] tobaccoes = new int[5];
000062       
000063        // 5*5的二维数组,答案就在其中:
000064        private int[][] key = { nationalities, colors, pets, drinks, tobaccoes };
000065       
000066        private static final int[][] values = { { 1, 2, 3, 4, 5 },
000067                { 1, 2, 3, 5, 4 }, { 1, 2, 4, 3, 5 }, { 1, 2, 4, 5, 3 },
000068                { 1, 2, 5, 3, 4 }, { 1, 2, 5, 4, 3 }, { 1, 3, 2, 4, 5 },
000069                { 1, 3, 2, 5, 4 }, { 1, 3, 4, 2, 5 }, { 1, 3, 4, 5, 2 },
000070                { 1, 3, 5, 2, 4 }, { 1, 3, 5, 4, 2 }, { 1, 4, 2, 3, 5 },
000071                { 1, 4, 2, 5, 3 }, { 1, 4, 3, 2, 5 }, { 1, 4, 3, 5, 2 },
000072                { 1, 4, 5, 2, 3 }, { 1, 4, 5, 3, 2 }, { 1, 5, 2, 3, 4 },
000073                { 1, 5, 2, 4, 3 }, { 1, 5, 3, 2, 4 }, { 1, 5, 3, 4, 2 },
000074                { 1, 5, 4, 2, 3 }, { 1, 5, 4, 3, 2 }, { 2, 1, 3, 4, 5 },
000075                { 2, 1, 3, 5, 4 }, { 2, 1, 4, 3, 5 }, { 2, 1, 4, 5, 3 },
000076                { 2, 1, 5, 3, 4 }, { 2, 1, 5, 4, 3 }, { 2, 3, 1, 4, 5 },
000077                { 2, 3, 1, 5, 4 }, { 2, 3, 4, 1, 5 }, { 2, 3, 4, 5, 1 },
000078                { 2, 3, 5, 1, 4 }, { 2, 3, 5, 4, 1 }, { 2, 4, 1, 3, 5 },
000079                { 2, 4, 1, 5, 3 }, { 2, 4, 3, 1, 5 }, { 2, 4, 3, 5, 1 },
000080                { 2, 4, 5, 1, 3 }, { 2, 4, 5, 3, 1 }, { 2, 5, 1, 3, 4 },
000081                { 2, 5, 1, 4, 3 }, { 2, 5, 3, 1, 4 }, { 2, 5, 3, 4, 1 },
000082                { 2, 5, 4, 1, 3 }, { 2, 5, 4, 3, 1 }, { 3, 1, 2, 4, 5 },
000083                { 3, 1, 2, 5, 4 }, { 3, 1, 4, 2, 5 }, { 3, 1, 4, 5, 2 },
000084                { 3, 1, 5, 2, 4 }, { 3, 1, 5, 4, 2 }, { 3, 2, 1, 4, 5 },
000085                { 3, 2, 1, 5, 4 }, { 3, 2, 4, 1, 5 }, { 3, 2, 4, 5, 1 },
000086                { 3, 2, 5, 1, 4 }, { 3, 2, 5, 4, 1 }, { 3, 4, 1, 2, 5 },
000087                { 3, 4, 1, 5, 2 }, { 3, 4, 2, 1, 5 }, { 3, 4, 2, 5, 1 },
000088                { 3, 4, 5, 1, 2 }, { 3, 4, 5, 2, 1 }, { 3, 5, 1, 2, 4 },
000089                { 3, 5, 1, 4, 2 }, { 3, 5, 2, 1, 4 }, { 3, 5, 2, 4, 1 },
000090                { 3, 5, 4, 1, 2 }, { 3, 5, 4, 2, 1 }, { 4, 1, 2, 3, 5 },
000091                { 4, 1, 2, 5, 3 }, { 4, 1, 3, 2, 5 }, { 4, 1, 3, 5, 2 },
000092                { 4, 1, 5, 2, 3 }, { 4, 1, 5, 3, 2 }, { 4, 2, 1, 3, 5 },
000093                { 4, 2, 1, 5, 3 }, { 4, 2, 3, 1, 5 }, { 4, 2, 3, 5, 1 },
000094                { 4, 2, 5, 1, 3 }, { 4, 2, 5, 3, 1 }, { 4, 3, 1, 2, 5 },
000095                { 4, 3, 1, 5, 2 }, { 4, 3, 2, 1, 5 }, { 4, 3, 2, 5, 1 },
000096                { 4, 3, 5, 1, 2 }, { 4, 3, 5, 2, 1 }, { 4, 5, 1, 2, 3 },
000097                { 4, 5, 1, 3, 2 }, { 4, 5, 2, 1, 3 }, { 4, 5, 2, 3, 1 },
000098                { 4, 5, 3, 1, 2 }, { 4, 5, 3, 2, 1 }, { 5, 1, 2, 3, 4 },
000099                { 5, 1, 2, 4, 3 }, { 5, 1, 3, 2, 4 }, { 5, 1, 3, 4, 2 },
000100                { 5, 1, 4, 2, 3 }, { 5, 1, 4, 3, 2 }, { 5, 2, 1, 3, 4 },
000101                { 5, 2, 1, 4, 3 }, { 5, 2, 3, 1, 4 }, { 5, 2, 3, 4, 1 },
000102                { 5, 2, 4, 1, 3 }, { 5, 2, 4, 3, 1 }, { 5, 3, 1, 2, 4 },
000103                { 5, 3, 1, 4, 2 }, { 5, 3, 2, 1, 4 }, { 5, 3, 2, 4, 1 },
000104                { 5, 3, 4, 1, 2 }, { 5, 3, 4, 2, 1 }, { 5, 4, 1, 2, 3 },
000105                { 5, 4, 1, 3, 2 }, { 5, 4, 2, 1, 3 }, { 5, 4, 2, 3, 1 },
000106                { 5, 4, 3, 1, 2 }, { 5, 4, 3, 2, 1 } };
000107       
000108        public void printKey() {
000109                for ( int i = 0; i < 5; i++) {
000110                print("nationality", key[0][i]);
000111                }
000112                System.out.println();
000113                for ( int i = 0; i < 5; i++) {
000114                print("color", key[1][i]);
000115                }
000116                System.out.println();
000117                for ( int i = 0; i < 5; i++) {
000118                print("pet", key[2][i]);
000119                }
000120                System.out.println();
000121                for ( int i = 0; i < 5; i++) {
000122                print("drink", key[3][i]);
000123                }
000124                System.out.println();
000125                for ( int i = 0; i < 5; i++) {
000126                print("tobacco", key[4][i]);
000127                }
000128                System.out.println();
000129       
000130        }
000131       
000132        private void print(String item, int index) {
000133                if (false) {
000134                } else if ("nationality".equals(item)) {
000135                switch (index) {
000136                case 1:
000137                        System.out.print("英国人\t");
000138                        break;
000139                case 2:
000140                        System.out.print("瑞典人\t");
000141                        break;
000142                case 3:
000143                        System.out.print("丹麦人\t");
000144                        break;
000145                case 4:
000146                        System.out.print("挪威人\t");
000147                        break;
000148                case 5:
000149                        System.out.print("德国人\t");
000150                        break;
000151                }
000152                } else if ("color".equals(item)) {
000153                switch (index) {
000154                case 1:
000155                        System.out.print("红房子\t");
000156                        break;
000157                case 2:
000158                        System.out.print("绿房子\t");
000159                        break;
000160                case 3:
000161                        System.out.print("黄房子\t");
000162                        break;
000163                case 4:
000164                        System.out.print("白房子\t");
000165                        break;
000166                case 5:
000167                        System.out.print("蓝房子\t");
000168                        break;
000169                }
000170                } else if ("pet".equals(item)) {
000171                switch (index) {
000172                case 1:
000173                        System.out.print("狗\t");
000174                        break;
000175                case 2:
000176                        System.out.print("鸟\t");
000177                        break;
000178                case 3:
000179                        System.out.print("猫\t");
000180                        break;
000181                case 4:
000182                        System.out.print("马\t");
000183                        break;
000184                case 5:
000185                        System.out.print("鱼\t");
000186                        break;
000187                }
000188                } else if ("drink".equals(item)) {
000189                switch (index) {
000190                case 1:
000191                        System.out.print("茶\t");
000192                        break;
000193                case 2:
000194                        System.out.print("咖啡\t");
000195                        break;
000196                case 3:
000197                        System.out.print("牛奶\t");
000198                        break;
000199                case 4:
000200                        System.out.print("啤酒\t");
000201                        break;
000202                case 5:
000203                        System.out.print("水\t");
000204                        break;
000205                }
000206                } else if ("tobacco".equals(item)) {
000207                switch (index) {
000208                case 1:
000209                        System.out.print("PALLMALL\t");
000210                        break;
000211                case 2:
000212                        System.out.print("DUNHILL\t");
000213                        break;
000214                case 3:
000215                        System.out.print("BLUEMASTER\t");
000216                        break;
000217                case 4:
000218                        System.out.print("PRINCE\t");
000219                        break;
000220                case 5:
000221                        System.out.print("Blue Master\t");
000222                        break;
000223                }
000224                }
000225        }
000226       
000227        // 条件1:英国人住在红房子里 01
000228        private boolean check01() {
000229                for ( int i = 0; i < nationalities.length; i++) {
000230                if (key[0][i] == NATIONALITY_ENGLISH) {
000231                        if (key[1][i] != COLOR_RED) {
000232                        return false;
000233                        } else {
000234                        return true;
000235                        }
000236                }
000237                }
000238                return false;
000239        }
000240       
000241        // 条件2:瑞典人养了一条狗 02
000242        private boolean check02() {
000243                for ( int i = 0; i < nationalities.length; i++) {
000244                if (key[0][i] == NATIONALITY_SWIDISH) {
000245                        if (key[2][i] != PET_DOG) {
000246                        return false;
000247                        } else {
000248                        return true;
000249                        }
000250                }
000251                }
000252                return false;
000253        }
000254       
000255        // 条件4:绿房子在白房子的左边 1
000256        private boolean check1() {
000257                for ( int i = 0; i < colors.length; i++) {
000258                if (key[1][i] == COLOR_GREEN) {
000259                        for ( int j = 0; j < colors.length; j++) {
000260                        if (key[1][j] == COLOR_WHITE) {
000261                                if (i > j) {
000262                                return false;
000263                                } else {
000264                                return true;
000265                                }
000266                        }
000267                        }
000268                }
000269                }
000270                return false;
000271        }
000272       
000273        // 条件8:住在中间房子的人喝牛奶 3
000274        private boolean check3() {
000275                return key[3][2] == DRINK_MILK ? true : false;
000276        }
000277       
000278        // 条件9:挪威人住在第一间房子 0
000279        private boolean check0() {
000280                if (key[0][0] != NATIONALITY_NORWAY) {
000281                return false;
000282                }
000283                return true;
000284        }
000285       
000286        // 14.挪威人住在蓝房子旁边 01
000287        private boolean check011() {
000288                for ( int i = 0; i < nationalities.length; i++) {
000289                if (key[0][i] == NATIONALITY_NORWAY) {
000290                        for ( int j = 0; j < colors.length; j++) {
000291                        if (key[1][j] == COLOR_BLUE) {
000292                                if (Math.abs(i - j) == 1) {
000293                                return true;
000294                                } else {
000295                                return false;
000296
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值