爱因斯坦谜题谁养鱼C#版Word格式文档下载.docx
《爱因斯坦谜题谁养鱼C#版Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《爱因斯坦谜题谁养鱼C#版Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
如果仅把上面的第二点提到的绿房子在白房子的左边,不限于紧邻,则会出现7组符合条件的组合,3个答案:
丹麦人养鱼(3组)、德国人养鱼(3组)、挪威人养鱼(1组)。
这显然不符合爱因斯坦谜题的本意,所以又查了查英文原题,结果真相大白,其严谨的表述有效的消除了以上的歧义。
那最终的结果究竟又如何呢?
英文原题:
TheEinsteinPuzzle
Thereare5housesinfivedifferentcolors.Theyarelinedupinarowsidebyside.
Ineachhouselivesapersonwithadifferentnationality.
These5ownersdrinkacertaindrink,smokeacertainbrandoftobaccoandkeepacertainpet.
Noownershavethesamepet,smokethesametobacco,ordrinkthesamedrink.
Asyoulookatthe5housesfromacrossthestreet,thegreenhouseisadjacent(woog注释:
adjacentadj,毗连的,邻近的,接近的;
n,近邻)totheleftofthewhitehouse
TheBigQuestionis:
WhoownstheFish?
CLUES:
1、TheBritlivesintheredhouse
2、TheSwedekeepsdogsaspets
3、TheDanedrinkstea
4、Thegreenhouseisontheimmediateleftofthewhitehouseasyoustareatthefrontofthe5houses
5、Thegreenhouseownerdrinkscoffee
6、ThepersonwhosmokesPallMallraisesbirds
7、TheowneroftheyellowhousesmokesDunhill
8、Themanlivinginthehouserightinthecenterdrinksmilk
9、TheNorwegianlivesinthefirsthouse
10、ThemanwhosmokesBlendslivesnexttotheonewhokeepscats
11、ThemanwhokeepshorseslivesnexttotheonewhosmokesDunhill
12、TheownerwhosmokesBluemasterdrinksjuice
13、TheGermansmokesPrince
14、TheNorwegianlivesnexttothebluehouse
15、ThemanwhosmokesBlendhasaneighborwhodrinkswater.
相关代码如下(考虑了两种情况,当#defineFastCompute时,仅有一组答案):
//[叶帆工作室]
#defineFastCompute
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Diagnostics;
namespaceEinstein
{
publicpartialclassfrmMain:
Form
{
publicfrmMain()
InitializeComponent();
}
privatevoidbtnRun_Click(objectsender,EventArgse)
{
Arithmeticarithmetic=newArithmetic();
DateTimedt=DateTime.Now;
stringresult=arithmetic.DoResult();
MessageBox.Show(result+"
\r\n耗时:
"
+(DateTime.Now-dt).TotalSeconds.ToString()+"
秒"
);
}
publicclassArithmetic
string[]people=newstring[]{"
英国"
"
瑞典"
丹麦"
挪威"
德国"
};
string[]house=newstring[]{"
红"
绿"
白"
黄"
蓝"
string[]drink=newstring[]{"
茶"
咖啡"
牛奶"
啤酒"
水"
string[]smoke=newstring[]{"
PallMall"
Dunhill"
Blends"
BlueMaster"
Prince"
string[]pet=newstring[]{"
狗"
鸟"
猫"
马"
鱼"
List<
string[]>
lstCombination=newList<
();
//存放全部结果(预删减后的结果)
lstCombination0=newList<
lstCombination1=newList<
lstCombination2=newList<
lstCombination3=newList<
lstCombination4=newList<
publicstringDoResult()
string[,]result=newstring[5,5];
//生成全部的组合
MakeCombination();
//预剔除不符合条件的组合
EliminateCombination();
//获得有可能的组合0
EliminateCombination0();
//获得有可能的组合1
EliminateCombination1();
//获得有可能的组合2
EliminateCombination2();
//获得有可能的组合3
EliminateCombination3();
//获得有可能的组合4
EliminateCombination4();
stringstrInfo="
;
intintNum=0;
for(inti=0;
i<
lstCombination0.Count;
i++)
ToCombination(result,0,lstCombination0,i);
for(intj=0;
j<
lstCombination1.Count;
j++)
ToCombination(result,1,lstCombination1,j);
for(intk=0;
k<
lstCombination2.Count;
k++)
ToCombination(result,2,lstCombination2,k);
for(intl=0;
l<
lstCombination3.Count;
l++)
ToCombination(result,3,lstCombination3,l);
for(intm=0;
m<
lstCombination4.Count;
m++)
ToCombination(result,4,lstCombination4,m);
boolFlag=true;
for(inte=0;
e<
5;
e++)
if(result[0,e]==result[1,e]||result[0,e]==result[2,e]||result[0,e]==result[3,e]||result[0,e]==result[4,e]||
result[1,e]==result[2,e]||result[1,e]==result[3,e]||result[1,e]==result[4,e]||
result[2,e]==result[3,e]||result[2,e]==result[4,e]||
result[3,e]==result[4,e])
Flag=false;
break;
//判断组合是否成立
if(Flag&
&
Judge(result))
strInfo+="
----------------"
+(++intNum).ToString()+"
----------------\r\n"
for(intii=0;
ii<
ii++)
for(intjj=0;
jj<
jj++)
strInfo+=result[ii,jj]+"
"
\r\n"
#ifFastCompute
------------------------------------\r\n"
returnstrInfo;
#endif
privatevoidToCombination(string[,]result,intindex,List<
lst,intnum)
i++)
result[index,i]=lst[num][i];
privatevoidMakeCombination()
string[]combination=newstring[5];
//5*5*5*5*5=3125
i++)//国籍
combination[0]=people[i];
for(intj=0;
j++)//房子
combination[1]=house[j];
k++)//饮料
combination[2]=drink[k];
for(intl=0;
l++)//香烟
combination[3]=smoke[l];
for(intm=0;
m++)//宠物
combination[4]=pet[m];
lstCombination.Add((string[])combination.Clone());
//剔除组合的判断条件
privateboolJudgeCombination(string[]combination)
//1、英国住红房子
if(combination[0]=="
&
combination[1]!
="
)returnfalse;
//2、瑞典养狗
combination[4]!
//3、丹麦喝茶
combination[2]!
//5、绿房子主喝咖啡
if(combination[1]=="
//6、抽PallMall香烟的养鸟
if(combination[3]=="
//7、黄房子主抽Dunhill香烟
combination[3]!
//12、抽BlueMaster的喝啤酒
//13、德国抽Prince香烟
returntrue;
privatevoidEliminateCombination()
string[]combination=newstring[5];
intnum=lstCombination.Count;
intindex=0;
while((num--)>
0)
if(!
JudgeCombination(lstCombination[index]))
lstCombination.RemoveAt(index);
else
index++;
//创建组合0
privatevoidEliminateCombination0()
lstCombination0.Clear();
foreach(string[]combinationinlstCombination)
//combination[1]!
combination[1]=="
)
#else
lstCombination0.Add(combination);
//创建组合1
privatevoidEliminateCombination1()
lstCombination1.Clear();
if(combination[0]!
lstCombination1.Add(combination);
//创建组合2
privatevoidEliminateCombination2()
lstCombination2.Clear();
combination[0]!
combination[2]=="
#else
lstCombination2.Add(combination);
//创建组合3
privatevoidEliminateCombination3()
lstCombination3.Clear();
lstCombination3.Add(combination);
//创建组合4
privatevoidEliminateCombination4()
lstCombination4.Clear();
lstCombination4.Add(combination);
//判断
privatestaticboolJudge(string[,]combination)
for(intindex=0;
index<
index++)
//4、绿房子在白房子左面
if(index>
0&
combination[index,1]=="
combination[index-1,1]!
if(combination[index,1]=="
for(inti=index+1;
if(combination[i,1]=="
)//绿房子不可能出现在白房子的右边
returnfalse;
#endif
//8、住在中间的喝牛奶
if(combination[2,2]!
//9、挪威住第一间房
if(combination[0,0]!
)retur