爱因斯坦在20世纪初出的这个谜语,题目是这样的:
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香烟的人有一个喝水的邻居
问题是:谁养鱼?
我用C来解决它
这个是个组合问题。
代码中有详细解释:
/// Einstein's Riddle ///
/// writed by kevin ///
/// crosof@126.com ///
/// 2004-12-10 ///
#include<stdio.h>
#include<string.h>
int a1,a2,a3,a4; ///计饮料
int b1,b2,b3,b4; ///计国籍
int c1,c2,c3,c4,c5; ///计颜色
int i,j;
int count = 0;
char country[5][15]={"挪威人", "瑞典人", "丹麦人","英国人", "德国人"};
char color[5][15]={"红色", "绿色","白色","蓝色","黄色"};
char drink[5][15]={"啤酒","水","咖啡","茶","牛奶"};
char pets[5][15]={"猫","狗","鸟","鱼","马"};
char tobaco[5][15]={"Prince", "Pall Mall", "Dunhill", "Blends", "Blue Master"};
char result[25][15]; ///记录一种组合结果序列
void subfeek()
{
int flagcolor1,flagcolor2,i;
英国人住在红房子里
if(!(strcmp(result[10],"英国人"))&&!(strcmp(result[11],"红色")));
else if(!(strcmp(result[15],"英国人"))&&!(strcmp(result[16],"红色")));
else if(!(strcmp(result[20],"英国人"))&&!(strcmp(result[21],"红色")));
else return ;
///绿房子在白房子左边
for(i=0;i<25;i++)
{
if(!(strcmp(result[i],"绿色")))
flagcolor1=i;
if(!(strcmp(result[i],"白色")))
flagcolor2=i;
}
if(flagcolor1>flagcolor2)
return ;
/// 瑞典人养了一条狗
for(i=5;i<21;i+=5)
{
if(!(strcmp(result[i],"瑞典人")))
strcpy(result[i+3],"狗");
}
/// 丹麦人喝茶
if(!(strcmp(result[5],"丹麦人"))&&!(strcmp(result[7],"茶")));
else if(!(strcmp(result[10],"丹麦人"))&&!(strcmp(result[12],"茶")));
else if(!(strcmp(result[15],"丹麦人"))&&!(strcmp(result[17],"茶")));
else if(!(strcmp(result[20],"丹麦人"))&&!(strcmp(result[22],"茶")));
else return ;
/// 绿房子主人喝咖啡
if(!(strcmp(result[1],"绿色"))&&!(strcmp(result[2],"咖啡")));
else if(!(strcmp(result[16],"绿色"))&&!(strcmp(result[17],"咖啡")));
else if(!(strcmp(result[21],"绿色"))&&(strcmp(result[22],"咖啡")));
else return ;
/// 德国人抽PRINCE烟
for(i=5;i<21;i+=5)
{
if(!(strcmp(result[i],"德国人")))
strcpy(result[i+4],"Prince");
}
/// 黄房子主人抽DUNHILL烟
for(i=1;i<21;i+=5)
{
if(!(strcmp(result[i],"黄色")))
if(!(strcmp(result[i+3],"")))
strcpy(result[i+3],"Dunhill");
else return;
}
/// 养马人住在DUNHILL烟的人旁边
if(!(strcmp(result[4],"Dunhill")))
if(!(strcmp(result[8],"")))
strcpy(result[8],"马");
else return;
if(!(strcmp(result[24],"Dunhill")))
if(!(strcmp(result[18],"")))
strcpy(result[18],"马");
else return;
for(i=9;i<20;i++)
{
if(!(strcmp(result[i],"Dunhill")))
{
if(!(strcmp(result[i-6],"")))
strcpy(result[i-6],"马");
else if(!(strcmp(result[i+4],"")))
strcpy(result[i+4],"马");
else return;
}
}
/// 抽BLUE MASTER烟的人喝啤酒
for(i=2;i<23;i++)
{
if(!(strcmp(result[i],"啤酒")))
strcpy(result[i+2],"Blue Master");
}
/// 抽Blends的人的邻居喝水
if(!(strcmp(result[2],"水")))
if(!(strcmp(result[9],"")))
strcpy(result[9],"Blends");
else return;
for(i=7;i<18;i++)
{
if(!(strcmp(result[i],"水")))
{
if(!(strcmp(result[i-3],"")))
strcpy(result[i-3],"Blends");
else if(!(strcmp(result[i+7],"")))
strcpy(result[i+7],"Blends");
else return ;
}
}
if(!(strcmp(result[22],"水")))
if(!(strcmp(result[19],"")))
strcpy(result[19],"Blends");
else return;
/// 抽Blends的人住在养猫人的旁边
if(!(strcmp(result[4],"Blends")))
if(!(strcmp(result[8],"")))
strcpy(result[8],"猫");
else return;
for(i=9;i<20;i++)
{
if(!(strcmp(result[i],"Blends")))
{
if(!(strcmp(result[i-6],"")))
strcpy(result[i-6],"猫");
else if(!(strcmp(result[i+4],"")))
strcpy(result[i+4],"猫");
else return ;
}
}
if(!(strcmp(result[24],"Blends")))
if(!(strcmp(result[18],"")))
strcpy(result[18],"猫");
else return;
/// 抽PALL MALL烟的人养了一只鸟
for(i=4;i<25;i+=5)
{
if(!(strcmp(result[i],""))&&!(strcmp(result[i-1],"")))
{
strcpy(result[i],"Pall Mall");
strcpy(result[i-1],"鸟");
break;
}
}
/// 养鱼的来了^_^
for(i=3;i<24;i+=5)
{
if(!(strcmp(result[i],"")))
strcpy(result[i],"鱼");
}
print the result!!!
j=0;
for(i=0;i<25;i++)
{
if(!(strcmp(result[i],"")))
j++;
}
if(j==0)
{
printf("一种正确组合:/n");
for(i=0;i<25;i+=5)
{
printf("住在 %s 房子里的 %s 喝 %4s 养%s 抽%s/n",result[i+1],result[i],result[i+2],result[i+3],result[i+4]);
}
printf("/n");
}
print the result!!!/
}
void main()
{
printf("Einstein's hints:/n"
" 1.) the Brit lives in the red house/n"
" 2.) the Swede keeps dogs as pets/n"
" 3.) the Dane drinks tea /n"
" 4.) the green house is on the left of the white house /n"
" 5.) the green house's owner drinks coffee /n"
" 6.) the person who smokes Pall Mall rears birds /n"
" 7.) the owner of the yellow house smokes Dunhill /n"
" 8.) the man living in the center house drinks milk /n"
" 9.) the Norwegian lives in the first house /n"
"10.) the man who smokes blends lives next to the one who keeps cats/n"
"11.) the man who keeps horses lives next to the man who smokes Dunhill /n"
"12.) the owner who smokes BlueMaster drinks beer /n"
"13.) the German smokes Prince /n"
"14.) the Norwegian lives next to the blue house /n"
"15.) the man who smokes blends has a neighbor who drinks water /n/n"
"The question is: Who owns the fish? /n"
);
//大循环开始
饮料//
strcpy(result[12],drink[4]);中间人喝牛奶
for(a1=0;a1<4;a1++)
{
strcpy(result[2],drink[a1]);
for(a2=0;a2<4;a2++)
{
if(a1==a2)
continue;
else
strcpy(result[7],drink[a2]);
for(a3=0;a3<4;a3++)
{
if((a3==a2)||(a3==a1))
continue;
else
strcpy(result[17],drink[a3]);
for(a4=0;a4<4;a4++)
{
if((a4==a3)||(a4==a2)||(a4==a1))
continue;
else
strcpy(result[22],drink[a4]);
/country
strcpy(result[0],country[0]); /第一个人是挪威人
for(b1=1;b1<5;b1++)
{
strcpy(result[5],country[b1]);
for(b2=1;b2<5;b2++)
{
if(b2==b1)
continue;
else
strcpy(result[10],country[b2]);
for(b3=1;b3<5;b3++)
{
if((b3==b2)||(b3==b1))
continue;
else
strcpy(result[15],country[b3]);
for(b4=1;b4<5;b4++)
{
if((b4==b1)||(b4==b2)||(b4==b3))
continue;
else
strcpy(result[20],country[b4]);
/房子颜色
c2=3;
for(c1=0;c1<5;c1++)
{
if(c2==c1)
continue;
else
{
strcpy(result[1],color[c1]);
strcpy(result[6],color[c2]);
}
for(c3=0;c3<5;c3++)
{
if((c3==c2)||(c3==c1))
continue;
else
strcpy(result[11],color[c3]);
for(c4=0;c4<5;c4++)
{
if((c4==c1)||(c4==c2)||(c4==c3))
continue;
else
strcpy(result[16],color[c4]);
for(c5=0;c5<5;c5++)
{
if((c5==c1)||(c5==c2)||(c5==c3)||(c5==c4))
continue;
else
{
strcpy(result[21],color[c5]);
subfeek();
清零
for(i=2;i<23;i+=5)
{
strcpy(result[i+1],"");
strcpy(result[i+2],"");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
///
printf("any key to escape ...");
getchar();
}
运行结果: