收藏 分销(赏)

爱因斯坦推理题C--实现.doc

上传人:仙人****88 文档编号:9450330 上传时间:2025-03-26 格式:DOC 页数:19 大小:239KB 下载积分:10 金币
下载 相关 举报
爱因斯坦推理题C--实现.doc_第1页
第1页 / 共19页
爱因斯坦推理题C--实现.doc_第2页
第2页 / 共19页


点击查看更多>>
资源描述
爱因斯坦在20世纪初出一道逻辑推理题。他说世界上有98%的人答不出来。 题目如下: 在一条街上,有5座房子,喷了5种颜色。每个房里住着不同国籍的人。每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。 已知条件: 1、英国人住红色房子 2、瑞典人养狗 3、丹麦人喝茶 4、绿色房子在白色房子左面 5、绿色房子主人喝咖啡 6、抽Pall Mall 香烟的人养鸟 7、黄色房子主人抽Dunhill 香烟 8、住在中间房子的人喝牛奶 9、 挪威人住第一间房 10、抽Blends香烟的人住在养猫的人隔壁 11、养马的人住抽Dunhill 香烟的人隔壁 12、抽Blue Master的人喝啤酒 13、德国人抽Prince香烟 14、挪威人住蓝色房子隔壁 15、抽Blends香烟的人有一个喝水的邻居 问谁养鱼? //--------------------------------------------------------------------------- 首先给出程序运行结果: //--------------------------------------------------------------------------- C++解析: 通过0-4来标记房子的位置,并规定最左边的位置的标号为0,最右边的标号为4。 通过0-4来标记国籍、房子颜色、饮料、宠物和烟的牌子。 通过排列组合的方法来选定他们的各种属性的组合。 由于需要组合的信息太大,选取先化简再进行组合的方法。 化简方法如下: 由条件8->条件9->条件14->条件4->条件5->条件1 可以确定的信息如下:  *位    置:    0         1          2          3          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__ //--------------------------------------------------------------------------- #define WIDTH 12 //--------------------------------------------------------------------------- #define YELLOW 0 //const #define BLUE 1 //const #define RED 2 //const #define GREEN 3 //const #define WHITE 4 //const //--------------------------------------------------------------------------- #define DOG 0 #define BIRD 1 #define CAT 2 #define HORSE 3 #define FISH 4 //--------------------------------------------------------------------------- #define PALLMALL 0 #define DUNHILL 1 #define BLENDS 2 #define BLUEMASTER 3 #define PRINCE 4 //--------------------------------------------------------------------------- #define DANISH 0 #define SWEDISH 1 #define GERMAN 2 #define NORWEGIAN 3 //const #define BRITISH 4 //const //--------------------------------------------------------------------------- #define TEA 0 #define BEER 1 #define WATER 2 #define COFFEE 3 //const #define MILK 4 //const //--------------------------------------------------------------------------- #endif //============================文件分割线======================================= /* *File: main.cpp *Author: Umbrella *Date: 2012/1/29/ 14:47 */ //--------------------------------------------------------------------------- /*位 置: 0 1 2 3 4 *国 籍: 挪威人 英国人 *房子颜色: 黄色 蓝色 红色 绿色 白色 *饮 料: 牛奶 咖啡 *宠 物: *烟的牌子: */ //--------------------------------------------------------------------------- #include <cstdlib> #include <iostream> #include<iomanip> #include <string> #include "MyDef.h" //--------------------------------------------------------------------------- using namespace std; //--------------------------------------------------------------------------- class cMan { public: int Nationality; int Pos; int Color; int Pet; int Drink; int Smoke; // cMan(int iPos) { this->Pos = iPos; this->Nationality = iPos; this->Color = iPos; this->Pet = iPos; this->Drink = iPos; this->Smoke = iPos; } }; //--------------------------------------------------------------------------- cMan *Man[5]; //类指针数组 int Num = 0; //答案个数 /* #define DANISH 0 #define SWEDISH 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 #define 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 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 */ string strSmoke[5] = {"PallMall", "Dunhill", "Blends", "BlueMaster", "Prince"}; //--------------------------------------------------------------------------- void ShowTest(); void Init(); //实例化对象,并初始化已经推断出来的信息 void End(); //释放对象 bool Check(); //判断是否满足剩余条件 bool Check2(); //2、瑞典人养狗 bool Check3(); //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香烟的人有一个喝水的邻居 void Calculate(); //进行计算 void NationalityCycle();//国籍循环 void DrinkCycle(); //饮料循环 void PetCycle(); //宠物循环 void SmokeCycle(); //香烟循环 void PrintInfo(); //输出结果 //--------------------------------------------------------------------------- int main(int argc, char *argv[]) { ShowTest(); Init(); Calculate(); End(); system("PAUSE"); return 0; } //--------------------------------------------------------------------------- void ShowTest() { cout<<"题目:"<<endl; cout<<"1、在一条街上,有5座房子,喷了5种颜色。"<<endl; cout<<"2、每个房里住着不同国籍的人。"<<endl; cout<<"3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。"<<endl; cout<<"条件:"<<endl; cout<<" 1、英国人住红色房子"<<endl; cout<<" 2、瑞典人养狗"<<endl; cout<<" 3、丹麦人喝茶"<<endl; cout<<" 4、绿色房子在白色房子左面"<<endl; cout<<" 5、绿色房子主人喝咖啡"<<endl; cout<<" 6、抽Pall Mall 香烟的人养鸟"<<endl; cout<<" 7、黄色房子主人抽Dunhill 香烟"<<endl; cout<<" 8、住在中间房子的人喝牛奶"<<endl; cout<<" 9、 挪威人住第一间房"<<endl; cout<<" 10、抽Blends香烟的人住在养猫的人隔壁"<<endl; cout<<" 11、养马的人住抽Dunhill 香烟的人隔壁"<<endl; cout<<" 12、抽Blue Master的人喝啤酒"<<endl; cout<<" 13、德国人抽Prince香烟"<<endl; cout<<" 14、挪威人住蓝色房子隔壁"<<endl; cout<<" 15、抽Blends香烟的人有一个喝水的邻居"<<endl; cout<<endl; cout<<" 问题是:谁养鱼?"<<endl; cout<<endl; return; } //--------------------------------------------------------------------------- void Init() { 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[0]->Nationality = NORWEGIAN; Man[2]->Nationality = BRITISH; // Man[2]->Drink = MILK; Man[3]->Drink = COFFEE; // return; } //--------------------------------------------------------------------------- void End() { int i; for(i=0; i<5; i++) { delete Man[i]; } cout<<"计算结束!"<<endl; cout<<endl; return; } //--------------------------------------------------------------------------- void Calculate() { NationalityCycle(); return; } //--------------------------------------------------------------------------- void NationalityCycle() { int i,j,k; for(i=0; i<3; i++) { if(i==0) { j = 1; } else { 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;} Man[1]->Nationality = i; Man[3]->Nationality = j; Man[4]->Nationality = k; DrinkCycle(); } } return; } //--------------------------------------------------------------------------- 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++) { 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]->Drink = k; PetCycle(); } } return; } //--------------------------------------------------------------------------- void PetCycle() //对宠物进行排列组合 { 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++) { 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;} 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;} 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){m=4;} Man[0]->Pet = i; Man[1]->Pet = j; Man[2]->Pet = k; Man[3]->Pet = l; Man[4]->Pet = m; SmokeCycle(); } } } } return; } //--------------------------------------------------------------------------- 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++) { 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;} 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;} 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){m=4;} Man[0]->Smoke = i; Man[1]->Smoke = j; Man[2]->Smoke = k; Man[3]->Smoke = l; Man[4]->Smoke = m; if(Check()) { PrintInfo(); } } } } } return; } //--------------------------------------------------------------------------- bool Check() //判断是否满足以下条件 { if(!Check2()) //2、瑞典人养狗 return false; else if(!Check3()) //3、丹麦人喝茶 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 if(!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香烟的人有一个喝水的邻居 return false; else return true; } //--------------------------------------------------------------------------- bool Check2() //2、瑞典人养狗 { int i; for(i=0; i<5; i++) { if(Man[i]->Nationality == SWEDISH) { if(Man[i]->Pet == DOG) return true; else return false; } else continue; } cout<<"Check2()出错!"<<endl; return false; } //--------------------------------------------------------------------------- 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; } else continue; } cout<<"Check3()出错!"<<endl; return false; } //--------------------------------------------------------------------------- bool Check6() //抽Pall Mall 香烟的人养鸟 { int i; for(i=0; i<5; i++) { if(Man[i]->Smoke == PALLMALL)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服