爱因斯坦谜语答案

这是一个关于爱因斯坦出的智商测试问题的解析,通过一系列逻辑推理,程序最终找到了所有可能的答案。在这个谜题中,涉及到五栋不同颜色的房子、五个不同国籍的主人、各种宠物、饮料和香烟品牌。经过复杂的条件匹配和排列组合,程序成功地生成了所有可能的解决方案,并输出到result.txt文件中。测试者需要找出哪位主人养的是鱼。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

爱因斯坦出的智商测试题, 他说世界上有98%的人回答不出, 看看你是否属于另外的2%:

    • 有5栋5种颜色的房子
    • 每一位房子的主人国籍都不同
    • 这5个人每人只喝一个牌子的饮料, 只抽一个牌子的香烟, 只养一种宠物
    • 没有人有相同的宠物, 抽相同牌子的香烟, 喝相同的饮料

已知:

    • 英国人住在红房子里
    • 瑞典人养了一条狗
    • 丹麦人喝茶
    • 绿房子在白房子左边
    • 绿房子主人喝咖啡
    • 抽PALL MALL烟的人养了一只鸟
    • 黄房子主人抽DUNHILL烟
    • 住在中间那间房子的人喝牛奶
    • 挪威人住在第一间房子
    • 抽混合烟的人住在养猫人的旁边
    • 养马人住在DUNHILL烟的人旁边
    • 抽BLUE MASTER烟的人喝啤酒
    • 德国人抽PRINCE烟
    • 挪威人住在蓝房子旁边
    • 抽混合烟的人的邻居喝矿泉水

  问题是: 谁养鱼?

下面是2002年大3时候写得答案,程序其实比较简单。

//--------------------answer.cpp--------------------------

#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#include <afx.h>
#include <dos.h>



unsigned long  num=1;
int result[5][5];
ofstream out;

void output(int i,int j)
{
    cout.setf (ios::left);
    cout.fill ('-');
    switch (i)
    {
    case 0:{switch (j)
           {
              case 0:{cout.width (14); out<<"RED        ";cout<<"RED";break;}
              case 1:{cout.width (14);out<< "GREEN      ";cout<<"GREEN";break;}
              case 2:{cout.width (14);out<< "WHITE      ";cout<<"WHITE";break;}
              case 3:{cout.width (14);out<< "BLUE       ";cout<<"BLUE";break;}
              case 4:{cout.width (14);out<< "YELLOW     ";cout<<"YELLOW";break;}
           };
            break;
           };
    case 1:{switch (j)
           {
              case 0:{cout.width (14);out<<"NORWEGIAN  ";cout<<"NORWEGIAN";break;}
              case 1:{cout.width (14);out<<"ENGLISH    ";cout<<"ENGLISH";break;}
              case 2:{cout.width (14);out<<"SWEDISH    ";cout<<"SWEDISH";break;}
              case 3:{cout.width (14);out<<"DANE       ";cout<<"DANE";break;}
              case 4:{cout.width (14);out<<"GERMAN     ";cout<<"GERMAN";break;}
           }
           ;break;};
    case 2:{switch (j)
           {
              case 0:{cout.width (14);out<<"DOG        ";cout<<"DOG";break;}
              case 1:{cout.width (14);out<<"BIRD       ";cout<<"BIRD";break;}
              case 2:{cout.width (14);out<<"HORSE      ";cout<<"HORSE";break;}
              case 3:{cout.width (14);out<<"CAT        ";cout<<"CAT";break;}
              case 4:{cout.width (14);out<<"FISH       ";cout<<"FISH";break;}
           }
           ;break;};
    case 3:{switch (j)
           {
              case 0:{cout.width (14);out<<"PALLMULL   ";cout<<"PALLMULL";break;}
              case 1:{cout.width (14);out<<"DUNHILL    ";cout<<"DUNHILL";break;}
              case 2:{cout.width (14);out<<"BLENDS     ";cout<<"BLENDS";break;}
              case 3:{cout.width (14);out<<"PRINCE     ";cout<<"PRINCE";break;}
              case 4:{cout.width (14);out<<"BLUEMASTER ";cout<<"BLUEMASTER";break;}
           }
           ;break;};
    case 4:{switch (j)
           {
              case 0:{cout.width (14);out<<"TEA        ";cout<<"TEA";break;}
              case 1:{cout.width (14);out<<"CAFFEE     ";cout<<"CAFFEE";break;}
              case 2:{cout.width (14);out<<"MILK       ";cout<<"MILK";break;}
              case 3:{cout.width (14);out<<"BEER       ";cout<<"BEER";break;}
              case 4:{cout.width (14);out<<"WATER      ";cout<<"WATER";break;}
           }
           ;break;};
    }


};

int locate (int j,int i)
{
  for (int a=0;a<5;a++)
   {
      if (result[j][a]==i) return a;

   };
  return -1;
};

main()
{//int accept=0;
 //while accept do
 // CTime time;
//  int Timeused = time.GetTime();
  struct  time time;
  gettime(&time);
  int secondused=time.ti_sec;
  int minuteused=time.ti_min;
 // printf("The current time is: %2d:%02d:%02d.%02d/n",
 //         time.ti_hour, time.ti_min, time.ti_sec, time.ti_hund);
  out.open ("result.txt");
  if (!out) {cout<<"Can't open this file!"<<endl;return(0);};

  for (int a=0;a<5;a++)
  for (int b=0;b<5;b++)
  for (int c=0;c<5;c++)
  for (int d=0;d<5;d++)
  for (int e=0;e<5;e++)
 // int a=1;int b=3;int c=0;int d=4;int e=2;
    if (a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e)
/*1*/    {result[0][0]=a; result[0][1]=b; result[0][2]=c ;result[0][3]=d;
         result[0][4]=e;
     //house :red ==0 green==1 white==2 blue==3 yellow==4
     //person:norway==0 english==1  swed==2 dan==3 germ==4
     //animal:   dog==0 bird==1 horse==2 cat==3  fish==4
     //smoke: pallmull==0 dunhill==1 blends==2 prince==3 blue master ==4
     //drink: tea==0 caffee==1 mild==2 beer==3 water==4
  //    int k=locate(0,1);int kk=locate(0,2);
      if (locate(0,1)<locate(0,2))
/*11*/{
       for (int aa=0;aa<5;aa++)
       for (int bb=0;bb<5;bb++)
       for (int cc=0;cc<5;cc++)
       for (int dd=0;dd<5;dd++)
       for (int ee=0;ee<5;ee++)
      // int aa=0;int bb=4;int cc=1;int dd=3;int ee=2;
          if (cc!=dd&&aa!=bb&&aa!=cc&&aa!=dd&&bb!=cc&&bb!=dd&&aa!=ee&&bb!=ee&&cc!=ee&&dd!=ee)
/* 2 */       {result[1][0]=aa;result[1][1]=bb;result[1][2]=cc;result[1][3]=dd;result[1][4]=ee;
         if (locate(1,0)==0)
         if (locate(0,0)==locate(1,1))
         if ((locate(0,3)==locate(1,0)+1)||(locate(0,3)==locate(1,0)-1))
/*21*/      {
          for (int aaa=0;aaa<5;aaa++)
          for (int bbb=0;bbb<5;bbb++)
          for (int ccc=0;ccc<5;ccc++)
          for (int ddd=0;ddd<5;ddd++)
          for (int eee=0;eee<5;eee++)
        //  int aaa=1;int bbb=3;int ccc=2;int ddd=4;int eee=0;
            if (aaa!=bbb&&aaa!=ccc&&aaa!=ddd&&aaa!=eee&&bbb!=ccc&&bbb!=ddd&&bbb!=eee&&ccc!=ddd&&ccc!=eee&&eee!=ddd)
/*   3 */        {result[2][0]=aaa; result[2][1]=bbb; result[2][2]=ccc;result[2][3]=ddd;
                result[2][4]=eee;
         if (locate(1,2)==locate(2,0))
 /*31*/          {
                for (int aaaa=0;aaaa<5;aaaa++)
               for (int bbbb=0;bbbb<5;bbbb++)
               for (int cccc=0;cccc<5;cccc++)
               for (int dddd=0;dddd<5;dddd++)
               for (int eeee=0;eeee<5;eeee++)
//               int aaaa=0;int bbbb=3;int cccc=2;int dddd=1;int eeee=4;
                  if (aaaa!=bbbb&&aaaa!=cccc&&aaaa!=dddd&&aaaa!=eeee&&bbbb!=cccc&&bbbb!=dddd&&bbbb!=eeee&&cccc!=dddd&&cccc!=eeee&&eeee!=dddd)
  /*   4 */           {result[3][0]=aaaa; result[3][1]=bbbb; result[3][2]=cccc;result[3][3]=dddd;
                 result[3][4]=eeee;
               if (locate(0,4)==locate(3,1))
               if (locate(2,1)==locate(3,0))
               if (locate(1,4)==locate(3,3))
               if ((locate(2,2)==locate(3,1)+1)||(locate(2,2)==locate(3,1)-1))
               if ((locate(2,3)==locate(3,2)+1)||(locate(2,3)==locate(3,2)-1))
/*41*/                 {
                 for (int aaaaa=0;aaaaa<5;aaaaa++)
                 for (int bbbbb=0;bbbbb<5;bbbbb++)
                 for (int ccccc=0;ccccc<5;ccccc++)
                 for (int ddddd=0;ddddd<5;ddddd++)
                 for (int eeeee=0;eeeee<5;eeeee++)
            //     int aaaaa=1;int bbbbb=4;int ccccc=2;int ddddd=0;int eeeee=3;
                    if (aaaaa!=bbbbb&&aaaaa!=ccccc&&aaaaa!=ddddd&&aaaaa!=eeeee&&bbbbb!=ccccc&&bbbbb!=ddddd&&bbbbb!=eeeee&&ccccc!=ddddd&&ccccc!=eeeee&&eeeee!=ddddd)
    /* 5 */                   {result[4][0]=aaaaa; result[4][1]=bbbbb; result[4][2]=ccccc;result[4][3]=ddddd;
                       result[4][4]=eeeee;
                      //   cout<<"1";
                       //     num++;

                      //contidion

              if (locate(0,1)==locate(4,1))
              if (locate(1,3)==locate(4,0))
              if ((locate(3,2)==locate(4,4)+1)||(locate(3,2)==locate(4,4)-1))
              if (locate(3,4)==locate(4,3))
              if (locate(4,2)==2)
                  {
                           printf("The answer %d:/n",num);
                           out<<"The answer "<<num++<<" :"<<endl;
                          // for (int r=0 ;r<5;r++, cout<<result[r][0]<<"        "<<result[r][1]<<"             "<<result[r][2]<<"           "<<result[r][3]<<"       "<<result[r][4]<<"     "<<endl);
                                      // cout.setf (ios::left);
                       out<< " 1     "<<"     2        "<<"  3       "<<"   4       "<<"   5       "<<endl;
                       cout<< " 1       "<<"       2        "<<"     3       "<<"      4       "<<"     5       "<<endl;
                       cout<<endl;
                       for(int d=0;d<5;d++)
                       {for(int b=0;b<5;b++)
                             output(d,result[d][b]);
                           cout<<endl;
                       out<<endl;
                       }
                                 };
   /*  5*/    };
   /*41*/    };
 /*  4  */  };
 /*31*/    };
/*3*/      };
/*21*/    };
/*2*/      };
/*11*/};
/*1*/ };

out<<"TOTAL:"<<num-1<<"/nThe end!/n";
printf("TOTAL:%d/nThe end!/n",num-1);
gettime(&time);
secondused=time.ti_sec-secondused;
minuteused=time.ti_min-minuteused;
minuteused=minuteused*60+secondused;
if (minuteused<1)  printf("Timeused:< 1 sec/n");
else printf("Timeused: %d sec/n",minuteused);
if (minuteused<1)  out<<"Timeused:< 1 sec/n";
else out<<"Timeused: "<<minuteused<<" sec"<<endl;
//printf("The current time is: %2d:%02d:%02d.%02d/n",
//          time.ti_hour, time.ti_min, time.ti_sec, time.ti_hund);
out.close();
cin>>minuteused;
return 0;

}

//-----------------------answer.h-------------------------------

//---------------------------------------------------------------------------

#ifndef answerH
#define answerH
//---------------------------------------------------------------------------
#endif

运行结果,但是好像是赛扬500

The answer 1 :
 1          2          3          4          5      
GREEN      BLUE       RED        YELLOW     WHITE     
NORWEGIAN  GERMAN     ENGLISH    DANE       SWEDISH   
BIRD       CAT        HORSE      FISH       DOG       
PALLMULL   PRINCE     BLENDS     DUNHILL    BLUEMASTER
CAFFEE     WATER      MILK       TEA        BEER      
The answer 2 :
 1          2          3          4          5      
GREEN      BLUE       RED        YELLOW     WHITE     
NORWEGIAN  GERMAN     ENGLISH    DANE       SWEDISH   
BIRD       FISH       HORSE      CAT        DOG       
PALLMULL   PRINCE     BLENDS     DUNHILL    BLUEMASTER
CAFFEE     WATER      MILK       TEA        BEER      
The answer 3 :
 1          2          3          4          5      
GREEN      BLUE       WHITE      RED        YELLOW    
NORWEGIAN  GERMAN     SWEDISH    ENGLISH    DANE      
BIRD       CAT        DOG        HORSE      FISH      
PALLMULL   PRINCE     BLENDS     BLUEMASTER DUNHILL   
CAFFEE     WATER      MILK       BEER       TEA       
The answer 4 :
 1          2          3          4          5      
GREEN      BLUE       WHITE      YELLOW     RED       
NORWEGIAN  GERMAN     SWEDISH    DANE       ENGLISH   
BIRD       CAT        DOG        FISH       HORSE     
PALLMULL   PRINCE     BLENDS     DUNHILL    BLUEMASTER
CAFFEE     WATER      MILK       TEA        BEER      
The answer 5 :
 1          2          3          4          5      
GREEN      BLUE       WHITE      YELLOW     RED       
NORWEGIAN  GERMAN     SWEDISH    DANE       ENGLISH   
BIRD       FISH       DOG        CAT        HORSE     
PALLMULL   PRINCE     BLENDS     DUNHILL    BLUEMASTER
CAFFEE     WATER      MILK       TEA        BEER      
The answer 6 :
 1          2          3          4          5      
GREEN      BLUE       YELLOW     RED        WHITE     
NORWEGIAN  GERMAN     SWEDISH    ENGLISH    DANE      
FISH       CAT        DOG        HORSE      BIRD      
BLENDS     PRINCE     DUNHILL    BLUEMASTER PALLMULL  
CAFFEE     WATER      MILK       BEER       TEA       
The answer 7 :
 1          2          3          4          5      
YELLOW     BLUE       RED        GREEN      WHITE     
NORWEGIAN  DANE       ENGLISH    GERMAN     SWEDISH   
CAT        HORSE      BIRD       FISH       DOG       
DUNHILL    BLENDS     PALLMULL   PRINCE     BLUEMASTER
WATER      TEA        MILK       CAFFEE     BEER      
TOTAL:7
The end!
Timeused:< 1 sec

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值