题目如下:
在一条街上,有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香烟的人有一个喝水的邻居
问谁养鱼?
//---------------------------------------------------------------------------
首先给出程序运行结果:
运算后 只有一种结果。
并发现第15个条件的有无并不影响结果。。。
//---------------------------------------------------------------------------
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)
{
if(Man[i]->Pet == BIRD)
return true;
else
return false;
}
else
continue;
}
cout<<"Check6()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check7()
//7、黄色房子主人抽Dunhill 香烟
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Color == YELLOW)
{
if(Man[i]->Smoke == DUNHILL)
return true;
else
return false;
}
else
continue;
}
cout<<"Check7()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check10()
//10、抽Blends香烟的人住在养猫的人隔壁
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Smoke == BLENDS)
{
if(i==0)
{
if(Man[1]->Pet == CAT)
return true;
else
return false;
}
if(i==4)
{
if(Man[3]->Pet == CAT)
return true;
else
return false;
}
else if((Man[i-1]->Pet == CAT) || (Man[i+1]->Pet == CAT))
{
return true;
}
else
return false;
}
else
continue;
}
cout<<"Check10()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check11()
//11、养马的人住抽Dunhill 香烟的人隔壁
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Pet == HORSE)
{
if(i==0)
{
if(Man[1]->Smoke == DUNHILL)
return true;
else
return false;
}
if(i==4)
{
if(Man[3]->Smoke == DUNHILL)
return true;
else
return false;
}
else if((Man[i-1]->Smoke == DUNHILL) || (Man[i+1]->Smoke == DUNHILL))
{
return true;
}
else
return false;
}
else
continue;
}
cout<<"Check11()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check12()
//12、抽Blue Master的人喝啤酒
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Smoke == BLUEMASTER)
{
if(Man[i]->Drink == BEER)
return true;
else
return false;
}
else
continue;
}
cout<<"Check12()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check13()
//13、德国人抽Prince香烟
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Nationality == GERMAN)
{
if(Man[i]->Smoke == PRINCE)
return true;
else
return false;
}
else
continue;
}
cout<<"Check13()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
bool Check15()
//15、抽Blends香烟的人有一个喝水的邻居
{
int i;
for(i=0; i<5; i++)
{
if(Man[i]->Smoke == BLENDS)
{
if(i==0)
{
if(Man[1]->Drink == WATER)
return true;
else
return false;
}
if(i==4)
{
if(Man[3]->Drink == WATER)
return true;
else
return false;
}
else if((Man[i-1]->Drink == WATER) || (Man[i+1]->Drink == WATER))
{
return true;
}
else
return false;
}
else
continue;
}
cout<<"Check15()出错!"<<endl;
return false;
}
//---------------------------------------------------------------------------
void PrintInfo()
{
int i;
Num++;
cout<<"==========================================================================="<<endl;
cout<<"第"<<Num<<"种可能:"<<endl;
cout<<"(最左边的编号为0,最右边的编号为4)"<<endl;
cout<<setw(9)<<"位置:"<<setw(WIDTH)<<0<<setw(WIDTH)<<setw(WIDTH)<<1
<<setw(WIDTH)<<2<<setw(WIDTH)<<3<<setw(WIDTH)<<4<<endl;
cout<<setw(9)<<"国籍:";
for(i=0; i<5; i++)
{
cout<<setw(WIDTH)<<strNationality[(Man[i]->Nationality)];
}
cout<<endl;
cout<<setw(9)<<"房子颜色:";
for(i=0; i<5; i++)
{
cout<<setw(WIDTH)<<strColor[Man[i]->Color];
}
cout<<endl;
cout<<setw(9)<<"饮料:";
for(i=0; i<5; i++)
{
cout<<setw(WIDTH)<<strDrink[Man[i]->Drink];
}
cout<<endl;
cout<<setw(9)<<"宠物:";
for(i=0; i<5; i++)
{
cout<<setw(WIDTH)<<strPet[Man[i]->Pet];
}
cout<<endl;
cout<<setw(9)<<"烟的牌子:";
for(i=0; i<5; i++)
{
cout<<setw(WIDTH)<<strSmoke[Man[i]->Smoke];
}
cout<<endl;
cout<<endl;
for(i=0; i<5; i++)
{
if(Man[i]->Pet == FISH)
{
cout<<setw(9)<<"答案为:";
cout<<setw(WIDTH)<<strNationality[Man[i]->Nationality]<<" 养鱼!"<<endl;
}
}
cout<<"==========================================================================="<<endl;
return;
}
//---------------------------------------------------------------------------