1、爱因斯坦在20世纪初出一道逻辑推理题。他说世界上有98%的人答不出来。 题目如下: 在一条街上,有5座房子,喷了5种颜色。每个房里住着不同国籍的人。每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。 已知条件: 1、英国人住红色房子 2、瑞典人养狗 3、丹麦人喝茶 4、绿色房子在白色房子左面 5、绿色房子主人喝咖啡 6、抽Pall Mall 香烟的人养鸟 7、黄色房子主人抽Dunhill 香烟 8、住在中间房子的人喝牛奶 9、 挪威人住第一间房 10、抽Blends香烟的人住在养猫的人隔壁 11、养马的人住抽Dunhill 香烟的人隔壁
2、 12、抽Blue Master的人喝啤酒 13、德国人抽Prince香烟 14、挪威人住蓝色房子隔壁 15、抽Blends香烟的人有一个喝水的邻居 问谁养鱼? //--------------------------------------------------------------------------- 首先给出程序运行结果: //--------------------------------------------------------------------------- C++解析: 通过0-4来标记房子的位置,并规定最左边的位置的标号为
3、0,最右边的标号为4。 通过0-4来标记国籍、房子颜色、饮料、宠物和烟的牌子。 通过排列组合的方法来选定他们的各种属性的组合。 由于需要组合的信息太大,选取先化简再进行组合的方法。 化简方法如下: 由条件8->条件9->条件14->条件4->条件5->条件1 可以确定的信息如下: *位 置: 0 1 2 3 4 *国 籍: 挪威人 英国人 *房子颜色: 黄色 蓝色 红色 绿色 白色
4、 *饮 料: 牛奶 咖啡 *宠 物: *烟的牌子: 通过排列组合后,同过检测其余的条件,来得到正确的结果。 由此给出C++实现的代码: //编译环境: DEV-C++ 4.9.9.2 //共两个文件:main.cpp和MyDef.h /* *File: MyDef.h *Author: Umbrella *Date: 2012/1/29/ 14:35 */ #ifndef __MY_DEFINE_H__ #define __MY_DEFINE_H
5、 //--------------------------------------------------------------------------- #define WIDTH 12 //--------------------------------------------------------------------------- #define YELLOW 0 //const #define BLUE 1 //const #define RED 2 //const #define GREEN 3
6、 //const #define WHITE 4 //const //--------------------------------------------------------------------------- #define DOG 0 #define BIRD 1 #define CAT 2 #define HORSE 3 #define FISH 4 //--------------------------------------------------------------------------- #d
7、efine PALLMALL 0 #define DUNHILL 1 #define BLENDS 2 #define BLUEMASTER 3 #define PRINCE 4 //--------------------------------------------------------------------------- #define DANISH 0 #define SWEDISH 1 #define GERMAN 2 #define
8、 NORWEGIAN 3 //const #define BRITISH 4 //const //--------------------------------------------------------------------------- #define TEA 0 #define BEER 1 #define WATER 2 #define COFFEE 3 //const #define MILK 4 //const //---------------------
9、 #endif //============================文件分割线======================================= /* *File: main.cpp *Author: Umbrella *Date: 2012/1/29/ 14:47 */ //---------------------------------------------------------------------------
10、/*位 置: 0 1 2 3 4 *国 籍: 挪威人 英国人 *房子颜色: 黄色 蓝色 红色 绿色 白色 *饮 料: 牛奶 咖啡 *宠 物: *烟的牌子: */ //---------------------------------------------------------------------------
11、
#include
12、c: int Nationality; int Pos; int Color; int Pet; int Drink; int Smoke; // cMan(int iPos) { this->Pos = iPos; this->Nationality = iPos; this->Color = iPos;
13、 this->Pet = iPos; this->Drink = iPos; this->Smoke = iPos; } }; //--------------------------------------------------------------------------- cMan *Man[5]; //类指针数组 int Num = 0; //答案个数 /* #define DANISH 0 #define SWEDI
14、SH 1 #define GERMAN 2 #define NORWEGIAN 3 //const #define BRITISH 4 //const */ string strNationality[5] = {"丹麦人", "瑞典人", "德国人", "挪威人", "英国人"}; /* #define YELLOW 0 //const #define BLUE 1 //const #define RED 2 //const #defi
15、ne GREEN 3 //const #define WHITE 4 //const */ string strColor[5] = {"黄色", "蓝色", "红色", "绿色", "白色"}; /* #define DOG 0 #define BIRD 1 #define CAT 2 #define HORSE 3 #define FISH 4 */ string strPet[5] = {"狗", "鸟", "猫", "马", "鱼"}; /* #define TEA 0 #define
16、BEER 1 #define WATER 2 #define COFFEE 3 //const #define MILK 4 //const */ string strDrink[5] = {"茶", "啤酒", "水", "咖啡", "牛奶"}; /* #define PALLMALL 0 #define DUNHILL 1 #define BLENDS 2 #define BLUEMASTER 3 #define PRINCE 4 */ strin
17、g strSmoke[5] = {"PallMall", "Dunhill", "Blends", "BlueMaster", "Prince"}; //--------------------------------------------------------------------------- void ShowTest(); void Init(); //实例化对象,并初始化已经推断出来的信息 void End(); //释放对象 bool Check(); //判断是否满足剩余条件 bool Check2(); //2、瑞典人养狗 bool Ch
18、eck3(); //3、丹麦人喝茶 bool Check6(); //6、抽Pall Mall 香烟的人养鸟 bool Check7(); //7、黄色房子主人抽Dunhill 香烟 bool Check10(); //10、抽Blends香烟的人住在养猫的人隔壁 bool Check11(); //11、养马的人住抽Dunhill 香烟的人隔壁 bool Check12(); //12、抽Blue Master的人喝啤酒 bool Check13(); //13、德国人抽Prince香烟 bool Check15(); //15、抽Blends香烟的人有一个喝水的邻居
19、void Calculate(); //进行计算 void NationalityCycle();//国籍循环 void DrinkCycle(); //饮料循环 void PetCycle(); //宠物循环 void SmokeCycle(); //香烟循环 void PrintInfo(); //输出结果 //--------------------------------------------------------------------------- int main(int argc, char *argv
20、[])
{
ShowTest();
Init();
Calculate();
End();
system("PAUSE");
return 0;
}
//---------------------------------------------------------------------------
void ShowTest()
{
cout<<"题目:"< 21、籍的人。"< 22、l 香烟的人养鸟"< 23、" 13、德国人抽Prince香烟"< 24、
int i;
for(i=0; i<5; i++)
{
Man[i] = new cMan(i);
}
//
Man[0]->Color = YELLOW;
Man[1]->Color = BLUE;
Man[2]->Color = RED;
Man[3]->Color = GREEN;
Man[4]->Color = WHITE;
//
Man[ 25、0]->Nationality = NORWEGIAN;
Man[2]->Nationality = BRITISH;
//
Man[2]->Drink = MILK;
Man[3]->Drink = COFFEE;
//
return;
}
//---------------------------------------------------------------------------
void End()
{
int i;
for( 26、i=0; i<5; i++)
{
delete Man[i];
}
cout<<"计算结束!"< 27、
void NationalityCycle()
{
int i,j,k;
for(i=0; i<3; i++)
{
if(i==0)
{
j = 1;
}
else
{
28、 j = 0;
}
for(j=0; j<3; j++)
{
if(j==i){continue;}
if(i!=0 && j!=0){k=0;}
else if(i!=1 && j!=1){k=1;}
else if(i!=2 && j!=2){k=2;}
29、
Man[1]->Nationality = i;
Man[3]->Nationality = j;
Man[4]->Nationality = k;
DrinkCycle();
}
}
return;
}
//- 30、
void DrinkCycle()
{
int i=0,j=0,k=0;
for(i=0; i<3; i++)
{
if(i==0){j = 1;}
else{j = 0;}
for(j=0; j<3; j++)
{
31、 if(j==i){continue;}
if(i!=0 && j!=0){k=0;}
else if(i!=1 && j!=1){k=1;}
else if(i!=2 && j!=2){k=2;}
Man[0]->Drink = i;
Man[1]->Drink = j;
Man[4]- 32、>Drink = k;
PetCycle();
}
}
return;
}
//---------------------------------------------------------------------------
void PetCycle()
//对宠物进行排列组合
{
int i,j,k,l,m;
for(i=0; i<5; i++)
{
if(i==0 33、){j = 1;}
else{j = 0;}
for(j=0; j<5; j++)
{
if(j==i){continue;}
if(j==0){k = 1;}
else{k = 0;}
for(k; k<5; k++)
{
34、 if(k==i || k==j){continue;}
if(k==0){l = 1;}
else{l = 0;}
for(l; l<5; l++)
{
if(l==i || l==j || l== k){continu 35、e;}
if(i!=0 && j!=0 && k!=0 && l!=0){m=0;}
else if(i!=1 && j!=1 && k!=1 && l!=1){m=1;}
else if(i!=2 && j!=2 && k!=2 && l!=2){m=2;}
else 36、if(i!=3 && j!=3 && k!=3 && l!=3){m=3;}
else if(i!=4 && j!=4 && k!=4 && l!=4){m=4;}
Man[0]->Pet = i;
Man[1]->Pet = j;
Man[2]->Pet = k;
37、
Man[3]->Pet = l;
Man[4]->Pet = m;
SmokeCycle();
}
}
}
}
return;
}
//------------- 38、
void SmokeCycle()
//对烟进行排列组合
{
int i,j,k,l,m;
for(i=0; i<5; i++)
{
if(i==0){j = 1;}
else{j = 0;}
for(j=0; j<5; j++)
{
39、 if(j==i){continue;}
if(j==0){k = 1;}
else{k = 0;}
for(k; k<5; k++)
{
if(k==i || k==j){continue;}
if(k==0){l = 1;}
40、 else{l = 0;}
for(l; l<5; l++)
{
if(l==i || l==j || l== k){continue;}
if(i!=0 && j!=0 && k!=0 && l!=0){m=0;}
41、 else if(i!=1 && j!=1 && k!=1 && l!=1){m=1;}
else if(i!=2 && j!=2 && k!=2 && l!=2){m=2;}
else if(i!=3 && j!=3 && k!=3 && l!=3){m=3;}
else if(i!=4 && j!=4 && k!=4 && l!=4 42、){m=4;}
Man[0]->Smoke = i;
Man[1]->Smoke = j;
Man[2]->Smoke = k;
Man[3]->Smoke = l;
Man[4]->S 43、moke = m;
if(Check())
{
PrintInfo();
}
}
44、 }
}
}
return;
}
//---------------------------------------------------------------------------
bool Check()
//判断是否满足以下条件
{
if(!Check2()) //2、瑞典人养狗
return false;
else if(!Check3()) //3、丹麦人喝茶
45、 return false;
else if(!Check6()) //6、抽Pall Mall 香烟的人养鸟
return false;
else if(!Check7()) //7、黄色房子主人抽Dunhill 香烟
return false;
else if(!Check10()) //10、抽Blends香烟的人住在养猫的人隔壁
return false;
else i 46、f(!Check11()) //11、养马的人住抽Dunhill 香烟的人隔壁
return false;
else if(!Check12()) //12、抽Blue Master的人喝啤酒
return false;
else if(!Check13()) //13、德国人抽Prince香烟
return false;
else if(!Check15()) //15、抽Blends香烟的人有一个喝水的邻 47、居
return false;
else
return true;
}
//---------------------------------------------------------------------------
bool Check2()
//2、瑞典人养狗
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Nationality == SWEDISH)
{
48、 if(Man[i]->Pet == DOG)
return true;
else
return false;
}
else
continue;
}
cout<<"Check2()出错!"< 49、
bool Check3()
//3、丹麦人喝茶
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Nationality == DANISH)
{
if(Man[i]->Drink == TEA)
return true;
else
return false;
}
50、 else
continue;
}
cout<<"Check3()出错!"<






