源程序(C文件)
/************************************************************************/
/*
已知:
1、在一条街上,有5座房子,喷了5种颜色。
2、每个房里住着不同国籍的人
3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物
问题是:谁养鱼?
提示:
1、英国人住红色房子
2、瑞典人养狗
3、丹麦人喝茶
4、绿色房子在白色房子左面
5、绿色房子主人喝咖啡
6、抽Pall Mall 香烟的人养鸟
7、黄色房子主人抽Dunhill 香烟
8、住在中间房子的人喝牛奶
9、 挪威人住第一间房
10、抽Blends香烟的人住在养猫的人隔壁
11、养马的人住抽Dunhill 香烟的人隔壁
12、抽Blue Master的人喝啤酒
13、德国人抽Prince香烟
14、挪威人住蓝色房子隔壁
15、抽Blends香烟的人有一个喝水的邻居 */
/************************************************************************/
#include "QuestionofEinsten.h"
RESOURCE_INFO_STRUCT gstResourceInfo = {0};
PERSON_INFO_STRUCT gastPerson[5]={0};
void main()
{
memset(&gstResourceInfo,0,sizeof(RESOURCE_INFO_STRUCT));
memset(&gastPerson,0,5*sizeof(RESOURCE_INFO_STRUCT));
initResource();
condition9();
getchar();
return;
}
void initResource()
{
char ucloop;
for(ucloop =1; ucloop <=5; ucloop++)
{
gstResourceInfo.aenCigarette[ucloop] = ucloop;
gstResourceInfo.aenColor[ucloop] = ucloop;
gstResourceInfo.aenDrink[ucloop] = ucloop;
gstResourceInfo.aenNationality[ucloop] = ucloop;
gstResourceInfo.aenPet[ucloop] = ucloop;
gstResourceInfo.auclocation[ucloop] = ucloop;
gastPerson[ucloop-1].uclocation = ucloop;
}
return;
}
/*9、 挪威人住第一间房*/
int condition9()
{
gastPerson[0].enNationality = Norwegian;
condition1();
return 0;
}
/*英国人住红色房子*/
int condition1()
{
int iReturnValue;
char ucloop;
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enNationality
&& 0 == gastPerson[ucloop].enColor)
{
gastPerson[ucloop].enNationality = Britisher;
gastPerson[ucloop].enColor = Red;
gstResourceInfo.aenNationality[Britisher] = 0;
gstResourceInfo.aenColor[Red] = 0;
iReturnValue = condition2();
if( 0 != iReturnValue)
{
gstResourceInfo.aenNationality[Britisher] = Britisher;
gstResourceInfo.aenColor[Red] = Red;
gastPerson[ucloop].enNationality = 0;
gastPerson[ucloop].enColor = 0;
}
else
{
continue;
/*break;*/
}
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*2、瑞典人养狗*/
int condition2()
{
int iReturnValue;
char ucloop;
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enNationality
&& 0 == gastPerson[ucloop].enPet)
{
gastPerson[ucloop].enNationality = Swede;
gastPerson[ucloop].enPet = Dog;
gstResourceInfo.aenNationality[Swede] = 0;
gstResourceInfo.aenPet[Dog] = 0;
iReturnValue = condition3();
if( 0 != iReturnValue)
{
gstResourceInfo.aenNationality[Swede] = Swede;
gstResourceInfo.aenPet[Dog] = Dog;
gastPerson[ucloop].enNationality = 0;
gastPerson[ucloop].enPet = 0;
}
else
{
continue;
}
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*3、丹麦人喝茶*/
int condition3()
{
int iReturnValue;
char ucloop;
if (gstResourceInfo.aenNationality[Dane] != Dane
||gstResourceInfo.aenDrink[Tea] != Tea)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enNationality
&& 0 == gastPerson[ucloop].enDrink)
{
gastPerson[ucloop].enNationality = Dane;
gastPerson[ucloop].enDrink = Tea;
gstResourceInfo.aenNationality[Dane] = 0;
gstResourceInfo.aenDrink[Tea] = 0;
iReturnValue = condition5();
if( 0 != iReturnValue)
{
gstResourceInfo.aenNationality[Dane] = Dane;
gstResourceInfo.aenDrink[Tea] = Tea;
gastPerson[ucloop].enNationality = 0;
gastPerson[ucloop].enDrink = 0;
}
else
{
continue;
}
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*5、绿色房子主人喝咖啡*/
int condition5()
{
int iReturnValue;
char ucloop;
if (gstResourceInfo.aenColor[Green] != Green
||gstResourceInfo.aenDrink[Coffee] != Coffee)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enDrink
&& 0 == gastPerson[ucloop].enColor)
{
gastPerson[ucloop].enColor = Green;
gastPerson[ucloop].enDrink = Coffee;
gstResourceInfo.aenColor[Green] = 0;
gstResourceInfo.aenDrink[Coffee] = 0;
iReturnValue = condition6();
if( 0 != iReturnValue)
{
gstResourceInfo.aenColor[Green] = Green;
gstResourceInfo.aenDrink[Coffee] = Coffee;
gastPerson[ucloop].enColor = 0;
gastPerson[ucloop].enDrink = 0;
}
else
{
continue;
}
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*6、抽Pall Mall 香烟的人养鸟*/
int condition6()
{
int iReturnValue;
char ucloop;
if (gstResourceInfo.aenCigarette[Pall_Mall] != Pall_Mall
||gstResourceInfo.aenPet[Bird] != Bird)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enPet
&& 0 == gastPerson[ucloop].enCigarette)
{
gastPerson[ucloop].enCigarette = Pall_Mall;
gastPerson[ucloop].enPet = Bird;
gstResourceInfo.aenCigarette[Pall_Mall] = 0;
gstResourceInfo.aenPet[Bird] = 0;
iReturnValue = condition7();
if( 0 != iReturnValue)
{
gstResourceInfo.aenCigarette[Pall_Mall] = Pall_Mall;
gstResourceInfo.aenPet[Bird] = Bird;
gastPerson[ucloop].enCigarette = 0;
gastPerson[ucloop].enPet = 0;
}
else
continue;
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*7、黄色房子主人抽Dunhill 香烟*/
int condition7()
{
int iReturnValue;
char ucloop;
if (gstResourceInfo.aenCigarette[Dunhill] != Dunhill
||gstResourceInfo.aenColor[Yellow] != Yellow)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enColor
&& 0 == gastPerson[ucloop].enCigarette)
{
gastPerson[ucloop].enCigarette = Dunhill;
gastPerson[ucloop].enColor = Yellow;
gstResourceInfo.aenCigarette[Dunhill] = 0;
gstResourceInfo.aenColor[Yellow] = 0;
iReturnValue = condition8();
if( 0 != iReturnValue)
{
gstResourceInfo.aenCigarette[Dunhill] = Dunhill;
gstResourceInfo.aenColor[Yellow] = Yellow;
gastPerson[ucloop].enCigarette = 0;
gastPerson[ucloop].enColor = 0;
}
else
continue;
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*8、住在中间房子的人喝牛奶*/
int condition8()
{
int iReturnValue;
if ( gastPerson[2].enDrink != 0
&&gastPerson[2].enDrink != Milk)
{
return -1;
}
if (gastPerson[2].enDrink == Milk)
{
iReturnValue = condition12();
if ( 0 != iReturnValue)
{
return -1;
}
return 0;
}
if ( gstResourceInfo.aenDrink[Milk] != Milk
&& gastPerson[2].enDrink != Milk)
{
return -1;
}
gastPerson[2].enDrink = Milk;
gstResourceInfo.aenDrink[Milk]=0;
iReturnValue = condition12();
if ( 0 != iReturnValue)
{
return -1;
}
return 0;
}
/*12、抽Blue Master的人喝啤酒*/
int condition12()
{
int iReturnValue;
char ucloop;
if ( gstResourceInfo.aenCigarette[Blue_Master] != Blue_Master
||gstResourceInfo.aenDrink[Beer] != Beer)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enDrink
&& 0 == gastPerson[ucloop].enCigarette)
{
gastPerson[ucloop].enCigarette = Blue_Master;
gastPerson[ucloop].enDrink = Beer ;
gstResourceInfo.aenCigarette[Blue_Master] = 0;
gstResourceInfo.aenDrink[Beer] = 0;
iReturnValue = condition13();
if( 0 != iReturnValue)
{
gstResourceInfo.aenCigarette[Blue_Master] = Blue_Master;
gstResourceInfo.aenDrink[Beer] = Beer;
gastPerson[ucloop].enCigarette = 0;
gastPerson[ucloop].enDrink = 0;
}
else
continue;
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}
/*13、德国人抽Prince香烟*/
int condition13()
{
int iReturnValue;
char ucloop;
if (gstResourceInfo.aenNationality[German] != German
||gstResourceInfo.aenCigarette[Prince] != Prince)
{
return -1;
}
for(ucloop = 0; ucloop < Number; ucloop++ )
{
if (0 == gastPerson[ucloop].enNationality
&& 0 == gastPerson[ucloop].enCigarette)
{
gastPerson[ucloop].enNationality = German;
gastPerson[ucloop].enCigarette = Prince;
gstResourceInfo.aenNationality[German] = 0;
gstResourceInfo.aenCigarette[Prince] = 0;
iReturnValue = condition10();
if( 0 != iReturnValue)
{
gstResourceInfo.aenNationality[German] = German;
gstResourceInfo.aenCigarette[Prince] = Prince;
gastPerson[ucloop].enNationality = 0;
gastPerson[ucloop].enCigarette = 0;
}
else
{
continue;
}
}
}
if (ucloop >= Number)
{
return -1;
}
return 0;
}