C#用两种方式破解号称世界上最难的问题!爱因斯坦在20世纪初出的谜语
源码下载地址:http://download.csdn.net/detail/devgis/4773299
问题:
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香烟的人有一个喝水的邻居
你们可以猜得出来吗?
算法1:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace AiYinSiTan
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] 饮料 = new string[4]{ "咖啡", "牛奶", "啤酒", "矿泉水" };
- string[] 香烟 = new string[4]{ "PALL MALL", "DUNHILL", "BLUE MASTER", "混合烟" };
- string[] 宠物 = new string[4]{ "鸟", "猫", "马", "鱼" };
- string[] 房子 = new string[4]{ "白", "绿", "黄", "蓝" };
- int[] 房子次序 = new int[4] { 2, 3, 4, 5 };
- //int counnum = 0;
- #region 穷举所有可能性
- for (int i = 0; i < 4; i++)//饮料
- {
- string 英国人饮料 = 饮料[i];
- for (int i1 = 0; i1 < 4; i1++)
- {
- if (饮料[i1] != 饮料[i])
- {
- string 瑞典人饮料 = 饮料[i1];
- for (int i2 = 0; i2 < 4; i2++)
- {
- if (饮料[i2] != 饮料[i] && 饮料[i2] != 饮料[i1] )
- {
- string 挪威人饮料 = 饮料[i2];
- for (int i3 = 0; i3 < 4; i3++)
- {
- if (饮料[i3] != 饮料[i] && 饮料[i3] != 饮料[i1] && 饮料[i3] != 饮料[i2])
- {
- string 德国人饮料 = 饮料[i3];
- for (int j = 0; j < 4; j++)
- {
- string 英国人香烟 = 香烟[j];
- for (int j1 = 0; j1 < 4; j1++)
- {
- if (香烟[j1] != 香烟[j])
- {
- string 瑞典人香烟 = 香烟[j1];
- for (int j2 = 0; j2 < 4; j2++)
- {
- if (香烟[j2] != 香烟[j] && 香烟[j2] != 香烟[j1])
- {
- string 挪威人香烟 = 香烟[j2];
- for (int j3 = 0; j3 < 4;j3++)
- {
- if (香烟[j3] != 香烟[j] && 香烟[j3] != 香烟[j1] && 香烟[j3] != 香烟[j2])
- {
- string 丹麦人香烟 = 香烟[j3];
- for (int l = 0; l < 4; l++)
- {
- string 英国人宠物 = 宠物[l];
- for (int l1 = 0; l1 < 4; l1++)
- {
- if (宠物[l1] != 宠物[l])
- {
- string 德国人宠物 = 宠物[l1];
- for (int l2 = 0; l2 < 4; l2++)
- {
- if (宠物[l2] != 宠物[l] && 宠物[l2] != 宠物[l1])
- {
- string 挪威人宠物 = 宠物[l2];
- for (int l3 = 0; l3 < 4; l3++)
- {
- if (宠物[l3] != 宠物[l] && 宠物[l3] != 宠物[l1] && 宠物[l3] != 宠物[l2])
- {
- string 丹麦人宠物 = 宠物[l3];
- for (int m = 0; m < 4; m++)
- {
- string 瑞典人房子 = 房子[m];
- for (int m1 = 0; m1 < 4; m1++)
- {
- if (房子[m1] != 房子[m])
- {
- string 德国人房子 = 房子[m1];
- for (int m2 = 0; m2 < 4; m2++)
- {
- if (房子[m2] != 房子[m] && 房子[m2] != 房子[m1])
- {
- string 挪威人房子 = 房子[m2];
- for (int m3 = 0; m3 < 4; m3++)
- {
- if (房子[m3] != 房子[m] && 房子[m3] != 房子[m1] && 房子[m3] != 房子[m2])
- {
- string 丹麦人房子 = 房子[m3];
- for (int n = 0; n < 4; n++)
- {
- int 英国人房子次序 = 房子次序[n];
- for (int n1 = 0; n1 < 4; n1++)
- {
- if (房子次序[n1] != 房子次序[n])
- {
- int 德国人房子次序 = 房子次序[n1];
- for (int n2 = 0; n2 < 4; n2++)
- {
- if (房子次序[n2] != 房子次序[n] && 房子次序[n2] != 房子次序[n1])
- {
- int 瑞典房子次序 = 房子次序[n2];
- for (int n3 = 0; n3 < 4; n3++)
- {
- if (房子次序[n3] != 房子次序[n] && 房子次序[n3] != 房子次序[n1] && 房子次序[n3] != 房子次序[n2])
- {
- int 丹麦人房子次序 = 房子次序[n3];
- #region 构造相关结果
- //counnum++;
- //if (counnum == 4801)
- //{
- // Console.WriteLine("ffffffffffff");
- //}
- //Console.WriteLine("第"+counnum.ToString()+"次操作!");
- //生成符合条件的对象
- Man 英国人啊 = new Man();
- 英国人啊.国籍 = "英国";
- 英国人啊.房子 = "红";
- 英国人啊.香烟 = 英国人香烟;
- 英国人啊.宠物 = 英国人宠物;
- 英国人啊.房子次序 = 英国人房子次序;
- 英国人啊.饮料 = 英国人饮料;
- Man 瑞典人啊 = new Man();
- 瑞典人啊.国籍 = "瑞典";
- 瑞典人啊.宠物 = "狗";
- 瑞典人啊.房子 = 瑞典人房子;
- 瑞典人啊.香烟 = 瑞典人香烟;
- 瑞典人啊.房子次序 = 瑞典房子次序;
- 瑞典人啊.饮料 = 瑞典人饮料;
- Man 丹麦人啊 = new Man();
- 丹麦人啊.国籍 = "丹麦";
- 丹麦人啊.饮料 = "茶";
- 丹麦人啊.房子 = 丹麦人房子;
- 丹麦人啊.香烟 = 丹麦人香烟;
- 丹麦人啊.宠物 = 丹麦人宠物;
- 丹麦人啊.房子次序 = 丹麦人房子次序;
- Man 挪威人啊 = new Man();
- 挪威人啊.国籍 = "挪威";
- 挪威人啊.房子次序 = 1;
- 挪威人啊.房子 = 挪威人房子;
- 挪威人啊.香烟 = 挪威人香烟;
- 挪威人啊.宠物 = 挪威人宠物;
- 挪威人啊.饮料 = 挪威人饮料;
- Man 德国人啊 = new Man();
- 德国人啊.国籍 = "德国";
- 德国人啊.香烟 = "PRINCE";
- 德国人啊.房子 = 德国人房子;
- 德国人啊.宠物 = 德国人宠物;
- 德国人啊.房子次序 = 德国人房子次序;
- 德国人啊.饮料 = 德国人饮料;
- ManContainer 老外合集 = new ManContainer();
- 老外合集.丹麦人 = 丹麦人啊;
- 老外合集.德国人 = 德国人啊;
- 老外合集.挪威人 = 挪威人啊;
- 老外合集.瑞典人 = 瑞典人啊;
- 老外合集.英国人 = 英国人啊;
- //验证对象是符合条件
- if (Check1(老外合集) && Check2(老外合集) && Check3(老外合集) && Check4(老外合集) && Check5(老外合集) && Check6(老外合集) && Check7(老外合集) && Check8(老外合集) && Check9(老外合集))
- {
- Man FuHeTiaoJian= GetManByChongWu("鱼", 老外合集);
- Console.WriteLine("养鱼的人是:" + FuHeTiaoJian.国籍 + "人,宠物是:" + FuHeTiaoJian.宠物 + " 房子是:" + FuHeTiaoJian.房子+"色的 抽的是:"+FuHeTiaoJian.香烟+"牌香烟 喝的是:"+FuHeTiaoJian.饮料+" 房子次序是:"+FuHeTiaoJian.房子次序);
- }
- #endregion
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- #endregion
- System.Console.Write("执行完毕!");
- System.Console.Read();
- }
- #region 判决方法
- /// <summary>
- /// 绿房子在白房子旁边,绿房子主人喝咖啡
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check1(ManContainer 一个老外)
- {
- Man temp1 = GetManByFangZi("绿", 一个老外);
- Man temp2 = GetManByFangZi("白", 一个老外);
- return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1 && temp1.饮料 == "咖啡");
- }
- /// <summary>
- /// 抽"PALL MALL"烟的人养 "鸟";
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check2(ManContainer 一个老外)
- {
- Man temp1 = GetManByYan("PALL MALL", 一个老外);
- return (temp1.宠物=="鸟");
- }
- /// <summary>
- /// 黄房子 主人抽 "DUNHILL"烟;
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check3(ManContainer 一个老外)
- {
- Man temp1 = GetManByFangZi("黄", 一个老外);
- return (temp1.香烟 == "DUNHILL");
- }
- /// <summary>
- /// 未知4.饮料 = "牛奶";未知4.房子次序 = 3; 中间房子的人喝牛奶
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check4(ManContainer 一个老外)
- {
- Man temp1 = GetManByHouseIndex(3, 一个老外);
- return (temp1.饮料 == "牛奶");
- }
- /// <summary>
- /// 未知5 抽混合烟的人住在样猫的人旁边!
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check5(ManContainer 一个老外)
- {
- Man temp1 = GetManByYan("混合烟", 一个老外);
- Man temp2 = GetManByChongWu("猫", 一个老外);
- return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);
- }
- /// <summary>
- /// 养马的人住在抽DUNHILL烟的旁边
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check6(ManContainer 一个老外)
- {
- Man temp1 = GetManByChongWu("马", 一个老外);
- Man temp2 = GetManByYan("DUNHILL", 一个老外);
- return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);
- }
- /// <summary>
- /// 抽BLUEMASTER烟的人喝啤酒
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check7(ManContainer 一个老外)
- {
- Man temp1 = GetManByYan("BLUE MASTER", 一个老外);
- return (temp1.饮料 == "啤酒");
- }
- /// <summary>
- /// 挪威人住在蓝房子旁边则 蓝房子 位置为2
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check8(ManContainer 一个老外)
- {
- Man temp1 = GetManByFangZi("蓝", 一个老外);
- return (temp1.房子次序 == 2);
- }
- /// <summary>
- /// 混合烟的邻居喝矿泉水
- /// </summary>
- /// <param name="老外合集"></param>
- /// <returns></returns>
- static public bool Check9(ManContainer 一个老外)
- {
- Man temp1 = GetManByYan("混合烟", 一个老外);
- Man temp2 = GetManByYingLiao("矿泉水", 一个老外);
- return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);
- }
- #endregion
- #region 通过一定条件获取人员
- /// <summary>
- /// 通过房子序号获取房主
- /// </summary>
- /// <param name="index"></param>
- /// <param name="一个老外"></param>
- /// <returns></returns>
- static public Man GetManByHouseIndex(int index, ManContainer 一个老外)
- {
- if (一个老外.丹麦人.房子次序 == index)
- {
- return 一个老外.丹麦人;
- }
- if (一个老外.德国人.房子次序 == index)
- {
- return 一个老外.德国人;
- }
- if (一个老外.挪威人.房子次序 == index)
- {
- return 一个老外.挪威人;
- }
- if (一个老外.瑞典人.房子次序 == index)
- {
- return 一个老外.瑞典人;
- }
- if (一个老外.英国人.房子次序 == index)
- {
- return 一个老外.英国人;
- }
- return null;
- }
- /// <summary>
- /// 抽某种烟的人
- /// </summary>
- /// <param name="index"></param>
- /// <param name="一个老外"></param>
- /// <returns></returns>
- static public Man GetManByYan(string 烟名, ManContainer 一个老外)
- {
- if (一个老外.丹麦人.香烟 == 烟名)
- {
- return 一个老外.丹麦人;
- }
- if (一个老外.德国人.香烟 == 烟名)
- {
- return 一个老外.德国人;
- }
- if (一个老外.挪威人.香烟 == 烟名)
- {
- return 一个老外.挪威人;
- }
- if (一个老外.瑞典人.香烟 == 烟名)
- {
- return 一个老外.瑞典人;
- }
- if (一个老外.英国人.香烟 == 烟名)
- {
- return 一个老外.英国人;
- }
- return null;
- }
- /// <summary>
- /// 养某种宠物
- /// </summary>
- /// <param name="index"></param>
- /// <param name="一个老外"></param>
- /// <returns></returns>
- static public Man GetManByChongWu(string 宠物名, ManContainer 一个老外)
- {
- if (一个老外.丹麦人.宠物 == 宠物名)
- {
- return 一个老外.丹麦人;
- }
- if (一个老外.德国人.宠物 == 宠物名)
- {
- return 一个老外.德国人;
- }
- if (一个老外.挪威人.宠物 == 宠物名)
- {
- return 一个老外.挪威人;
- }
- if (一个老外.瑞典人.宠物 == 宠物名)
- {
- return 一个老外.瑞典人;
- }
- if (一个老外.英国人.宠物 == 宠物名)
- {
- return 一个老外.英国人;
- }
- return null;
- }
- /// <summary>
- /// 住某种颜色房子的
- /// </summary>
- /// <param name="index"></param>
- /// <param name="一个老外"></param>
- /// <returns></returns>
- static public Man GetManByFangZi(string 房子颜色, ManContainer 一个老外)
- {
- if (一个老外.丹麦人.房子 == 房子颜色)
- {
- return 一个老外.丹麦人;
- }
- if (一个老外.德国人.房子 == 房子颜色)
- {
- return 一个老外.德国人;
- }
- if (一个老外.挪威人.房子 == 房子颜色)
- {
- return 一个老外.挪威人;
- }
- if (一个老外.瑞典人.房子 == 房子颜色)
- {
- return 一个老外.瑞典人;
- }
- if (一个老外.英国人.房子 == 房子颜色)
- {
- return 一个老外.英国人;
- }
- return null;
- }
- /// <summary>
- /// 喝某种饮料的人
- /// </summary>
- /// <param name="index"></param>
- /// <param name="一个老外"></param>
- /// <returns></returns>
- static public Man GetManByYingLiao(string 饮料名称, ManContainer 一个老外)
- {
- if (一个老外.丹麦人.饮料 == 饮料名称)
- {
- return 一个老外.丹麦人;
- }
- if (一个老外.德国人.饮料 == 饮料名称)
- {
- return 一个老外.德国人;
- }
- if (一个老外.挪威人.饮料 == 饮料名称)
- {
- return 一个老外.挪威人;
- }
- if (一个老外.瑞典人.饮料 == 饮料名称)
- {
- return 一个老外.瑞典人;
- }
- if (一个老外.英国人.饮料 == 饮料名称)
- {
- return 一个老外.英国人;
- }
- return null;
- }
- #endregion
- }
- #region 对象模型
- //人员模型
- public class Man:Object
- {
- #region 属性
- private string guoji="";
- public string 国籍
- {
- get
- {
- return guoji;
- }
- set
- {
- guoji = value;
- }
- }
- private string yinliao = "";
- public string 饮料
- {
- get
- {
- return yinliao;
- }
- set
- {
- yinliao = value;
- }
- }
- private string xiangyan = "";
- public string 香烟
- {
- get
- {
- return xiangyan;
- }
- set
- {
- xiangyan = value;
- }
- }
- private string chongwu = "";
- public string 宠物
- {
- get
- {
- return chongwu;
- }
- set
- {
- chongwu = value;
- }
- }
- private string fangzi = "";
- public string 房子
- {
- get
- {
- return fangzi;
- }
- set
- {
- fangzi = value;
- }
- }
- private int fangziindex=0;
- public int 房子次序
- {
- get
- {
- return fangziindex;
- }
- set
- {
- fangziindex = value;
- }
- }
- #endregion
- #region 方法
- public override bool Equals(object obj)
- {
- return base.Equals(obj);
- }
- public void Copy(Man a)
- {
- this.国籍 = a.国籍;
- this.香烟 = a.香烟;
- this.饮料 = a.饮料;
- this.宠物 = a.宠物;
- this.房子 = a.房子;
- }
- #endregion
- }
- //业务模型,为结果的一种可能性
- public class ManContainer
- {
- public Man 英国人;
- public Man 瑞典人;
- public Man 丹麦人;
- public Man 挪威人;
- public Man 德国人;
- }
- #endregion
- }
算法2(中软一个同事用C++写的 本人借用到C# ):
using System;
using System.Collections.Generic;
using System.Text;
namespace AiYinSiTan2
{
struct House{
public string nation;
public string color;
public string pet;
public string drink;
public string cigarette;
};
class Program
{
static void Main(string[] args)
{
string[] Title = new string[5] { "国籍", "颜色", "宠物", "饮料", "香烟" };
House house0= new House();
House house1 = new House();
House house2 = new House();
House house3 = new House();
House house4 = new House();
House[] HouseMan = new House[5] { new House(), new House(), new House(),new House(), new House() };//房子从左到右编号
HouseMan[0] = house0;
HouseMan[1] = house1;
HouseMan[2] = house2;
HouseMan[3] = house3;
HouseMan[4] = house4;
HouseMan[0].nation = "挪威";//挪威人住在第一个房子里面
HouseMan[1].color = "蓝色";//挪威人和住在蓝房子的人相邻,即蓝房子是第2个房子
HouseMan[2].drink = "牛奶";// 住在中间那个房子里的人喝牛奶
for (int e = 1; e < 5; e++) //表示英国人的房子序号
{
for (int r = 1; r < 5;r++)//表示瑞典人的房子序号
{
for (int d = 1; d < 5;d++)//表示丹麦人的房子序号
{
for (int g = 0; g <5; g++)//表示绿房子的序号
{
for (int p = 0; p< 5; p++)//表示抽Pall Mall牌香烟的人的房子序号
{
for (int y = 0;y < 5; y++)//表示黄色房子的序号
{
for (intb=0; b<5; b++)//表示喝啤酒人的房子序号
{
for (int h =0; h < 5; h++)//表示养马的人的房子序号
{
for(int cat = 0; cat < 5; cat++)//表示养猫的人的房子序号
{
int Germany = 10 - 0 - e - r - d; //表示德国人的房子序号,德国人抽Prince牌香烟;
int Blends = 10 - p - y - b - Germany;//表示抽Blends牌香烟的人的房子序号
int white = 10 - 1 - e - g -y; //表示白房子的序号
int water = 10 - 2 - d - g - b; //表示喝矿泉水的人的房子序号
int fish = 10 - r - p - h - cat;//表示养鱼的人的房子序号
bool A1 = (e != 1); //英国人住在红色的房子里;根据房子和国家判断
bool A2 = (r != e); //瑞典人养狗作为宠物;根据国家和宠物判断
bool A3 = (d != e && d != r && d != 2);//丹麦人喝茶;根据国家和饮料判断
bool A4 = (g != 1 && g != 2 && g != e && g !=d);//绿房子的主人喝咖啡;根据颜色和饮料判断
bool A5 = (p != r); //抽Pall Mall牌香烟的人养鸟;根据香烟和宠物判断
bool A6 = (y != e && y != 1 && y != g && y !=p);//黄色房子里的人抽Dunhill牌香烟;根据颜色和香烟判断
bool A7 = (b != 2 && b != d && b != g && b != p&& b != y);//抽BlueMaster牌香烟的人喝啤酒;根据香烟和饮料判断
bool A8 = (h != r && h != p && (h == y + 1 || h == y -1));//养马的人和抽Dunhill牌香烟的人相邻,根据香烟和宠物判断
bool A9 = (white == g + 1); //绿房子紧挨着白房子,在白房子的左边;
bool A0 = (cat == Blends - 1 || cat == Blends + 1);//Blends牌香烟的人和养猫的人相邻;
bool A11 = (water == Blends - 1 || water == Blends + 1);//抽Blends牌香烟的人和喝矿泉水的人相邻
if (A1 && A2 && A3 && A4 && A5&& A6 && A7 && A8 && A9 && A0&& A11)
{//把满足条件的序号填入结构数组
HouseMan[e].nation = "英国";
HouseMan[e].color = "红色";
HouseMan[r].nation = "瑞典";
HouseMan[r].pet = "狗";
HouseMan[d].nation= "丹麦";
HouseMan[d].drink = "茶";
HouseMan[g].color = "绿色";
HouseMan[g].drink ="咖啡";
HouseMan[p].cigarette = "Pall Mall";
HouseMan[p].pet = "鸟";
HouseMan[y].color = "黄色";
HouseMan[y].cigarette = "Dunhill";
HouseMan[b].cigarette = "BlueMaster";
HouseMan[b].drink = "啤酒";
HouseMan[h].pet = "马";
HouseMan[Germany].nation = "德国";
HouseMan[Germany].cigarette ="Prince";
HouseMan[Blends].cigarette = "Blends";
HouseMan[white].color = "白色";
HouseMan[water].drink = "矿泉水";
HouseMan[cat].pet = "猫";
HouseMan[fish].pet = "鱼";
Console.WriteLine("-------------------符合条件的方案----------------------------");
for (int i = 0; i < 5; i++)
{
Console.WriteLine(HouseMan[i].nation);
Console.WriteLine("------------------------------------");
Console.WriteLine(HouseMan[i].cigarette);
Console.WriteLine(HouseMan[i].color);
Console.WriteLine(HouseMan[i].drink);
Console.WriteLine(HouseMan[i].pet);
Console.WriteLine("----------------------------------------------------");
}
}
}
}
}
}
}
}
}
}
}
Console.Read();
}
}
//end:
// for (int i=0; i<5; i++)
// cout << Title << "\t";
// cout << endl << endl;
// for (int i=0; i<5; i++)
// {
// cout << House.nation << "\t";
// cout << House.color << "\t";
// cout << House.pet << "\t";
// cout << House.drink << "\t";
// cout << House.cigarette << "\t";
// cout << endl;
// }
}
有钱的朋友上我的CSDN去奉献点分吧,极品资源很多哦!:http://download.csdn.net/user/devgis