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:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Collections;  
  5. namespace AiYinSiTan  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             string[] 饮料 = new string[4]{ "咖啡""牛奶""啤酒""矿泉水" };  
  12.             string[] 香烟 = new string[4]{ "PALL MALL""DUNHILL""BLUE MASTER""混合烟" };  
  13.             string[] 宠物 = new string[4]{ "鸟""猫""马""鱼" };  
  14.             string[] 房子 = new string[4]{ "白""绿""黄""蓝" };  
  15.             int[] 房子次序 = new int[4] { 2, 3, 4, 5 };  
  16.   
  17.             //int counnum = 0;  
  18.             #region 穷举所有可能性  
  19.             for (int i = 0; i < 4; i++)//饮料  
  20.             {  
  21.                 string 英国人饮料 = 饮料[i];  
  22.                 for (int i1 = 0; i1 < 4; i1++)  
  23.                 {  
  24.                     if (饮料[i1] != 饮料[i])  
  25.                     {  
  26.                         string 瑞典人饮料 = 饮料[i1];  
  27.                         for (int i2 = 0; i2 < 4; i2++)  
  28.                         {  
  29.                             if (饮料[i2] != 饮料[i] && 饮料[i2] != 饮料[i1] )  
  30.                             {  
  31.                                 string 挪威人饮料 = 饮料[i2];  
  32.                                 for (int i3 = 0; i3 < 4; i3++)  
  33.                                 {  
  34.                                     if (饮料[i3] != 饮料[i] && 饮料[i3] != 饮料[i1] && 饮料[i3] != 饮料[i2])  
  35.                                     {  
  36.                                         string 德国人饮料 = 饮料[i3];  
  37.                                         for (int j = 0; j < 4; j++)  
  38.                                         {  
  39.                                             string 英国人香烟 = 香烟[j];  
  40.                                             for (int j1 = 0; j1 < 4; j1++)  
  41.                                             {  
  42.                                                 if (香烟[j1] != 香烟[j])  
  43.                                                 {  
  44.                                                     string 瑞典人香烟 = 香烟[j1];  
  45.                                                     for (int j2 = 0; j2 < 4; j2++)  
  46.                                                     {  
  47.                                                         if (香烟[j2] != 香烟[j] && 香烟[j2] != 香烟[j1])  
  48.                                                         {  
  49.                                                             string 挪威人香烟 = 香烟[j2];  
  50.                                                             for (int j3 = 0; j3 < 4;j3++)  
  51.                                                             {  
  52.                                                                 if (香烟[j3] != 香烟[j] && 香烟[j3] != 香烟[j1] && 香烟[j3] != 香烟[j2])  
  53.                                                                 {  
  54.                                                                     string 丹麦人香烟 = 香烟[j3];  
  55.                                                                     for (int l = 0; l < 4; l++)  
  56.                                                                     {  
  57.                                                                         string 英国人宠物 = 宠物[l];  
  58.                                                                         for (int l1 = 0; l1 < 4; l1++)  
  59.                                                                         {  
  60.                                                                             if (宠物[l1] != 宠物[l])  
  61.                                                                             {  
  62.                                                                                 string 德国人宠物 = 宠物[l1];  
  63.                                                                                 for (int l2 = 0; l2 < 4; l2++)  
  64.                                                                                 {  
  65.                                                                                     if (宠物[l2] != 宠物[l] && 宠物[l2] != 宠物[l1])  
  66.                                                                                     {  
  67.                                                                                         string 挪威人宠物 = 宠物[l2];  
  68.                                                                                         for (int l3 = 0; l3 < 4; l3++)  
  69.                                                                                         {  
  70.                                                                                             if (宠物[l3] != 宠物[l] && 宠物[l3] != 宠物[l1] && 宠物[l3] != 宠物[l2])  
  71.                                                                                             {  
  72.                                                                                                 string 丹麦人宠物 = 宠物[l3];  
  73.                                                                                                 for (int m = 0; m < 4; m++)  
  74.                                                                                                 {  
  75.                                                                                                     string 瑞典人房子 = 房子[m];  
  76.                                                                                                     for (int m1 = 0; m1 < 4; m1++)  
  77.                                                                                                     {  
  78.                                                                                                         if (房子[m1] != 房子[m])  
  79.                                                                                                         {  
  80.                                                                                                             string 德国人房子 = 房子[m1];  
  81.                                                                                                             for (int m2 = 0; m2 < 4; m2++)  
  82.                                                                                                             {  
  83.                                                                                                                 if (房子[m2] != 房子[m] && 房子[m2] != 房子[m1])  
  84.                                                                                                                 {  
  85.                                                                                                                     string 挪威人房子 = 房子[m2];  
  86.                                                                                                                     for (int m3 = 0; m3 < 4; m3++)  
  87.                                                                                                                     {  
  88.                                                                                                                         if (房子[m3] != 房子[m] && 房子[m3] != 房子[m1] && 房子[m3] != 房子[m2])  
  89.                                                                                                                         {  
  90.                                                                                                                             string 丹麦人房子 = 房子[m3];  
  91.                                                                                                                             for (int n = 0; n < 4; n++)  
  92.                                                                                                                             {  
  93.                                                                                                                                 int 英国人房子次序 = 房子次序[n];  
  94.                                                                                                                                 for (int n1 = 0; n1 < 4; n1++)  
  95.                                                                                                                                 {  
  96.                                                                                                                                     if (房子次序[n1] != 房子次序[n])  
  97.                                                                                                                                     {  
  98.                                                                                                                                         int 德国人房子次序 = 房子次序[n1];  
  99.                                                                                                                                         for (int n2 = 0; n2 < 4; n2++)  
  100.                                                                                                                                         {  
  101.                                                                                                                                             if (房子次序[n2] != 房子次序[n] && 房子次序[n2] != 房子次序[n1])  
  102.                                                                                                                                             {  
  103.                                                                                                                                                 int 瑞典房子次序 = 房子次序[n2];  
  104.                                                                                                                                                 for (int n3 = 0; n3 < 4; n3++)  
  105.                                                                                                                                                 {  
  106.                                                                                                                                                     if (房子次序[n3] != 房子次序[n] && 房子次序[n3] != 房子次序[n1] && 房子次序[n3] != 房子次序[n2])  
  107.                                                                                                                                                     {  
  108.                                                                                                                                                         int 丹麦人房子次序 = 房子次序[n3];  
  109.  
  110.                                                                                                                                                         #region 构造相关结果  
  111.                                                                                                                                                         //counnum++;  
  112.                                                                                                                                                         //if (counnum == 4801)  
  113.                                                                                                                                                         //{  
  114.                                                                                                                                                         //    Console.WriteLine("ffffffffffff");  
  115.                                                                                                                                                         //}  
  116.                                                                                                                                                         //Console.WriteLine("第"+counnum.ToString()+"次操作!");  
  117.                                                                                                                                                         //生成符合条件的对象  
  118.                                                                                                                                                         Man 英国人啊 = new Man();  
  119.                                                                                                                                                         英国人啊.国籍 = "英国";  
  120.                                                                                                                                                         英国人啊.房子 = "红";  
  121.                                                                                                                                                         英国人啊.香烟 = 英国人香烟;  
  122.                                                                                                                                                         英国人啊.宠物 = 英国人宠物;  
  123.                                                                                                                                                         英国人啊.房子次序 = 英国人房子次序;  
  124.                                                                                                                                                         英国人啊.饮料 = 英国人饮料;  
  125.   
  126.                                                                                                                                                         Man 瑞典人啊 = new Man();  
  127.                                                                                                                                                         瑞典人啊.国籍 = "瑞典";  
  128.                                                                                                                                                         瑞典人啊.宠物 = "狗";  
  129.                                                                                                                                                         瑞典人啊.房子 = 瑞典人房子;  
  130.                                                                                                                                                         瑞典人啊.香烟 = 瑞典人香烟;  
  131.                                                                                                                                                         瑞典人啊.房子次序 = 瑞典房子次序;  
  132.                                                                                                                                                         瑞典人啊.饮料 = 瑞典人饮料;  
  133.   
  134.                                                                                                                                                         Man 丹麦人啊 = new Man();  
  135.                                                                                                                                                         丹麦人啊.国籍 = "丹麦";  
  136.                                                                                                                                                         丹麦人啊.饮料 = "茶";  
  137.                                                                                                                                                         丹麦人啊.房子 = 丹麦人房子;  
  138.                                                                                                                                                         丹麦人啊.香烟 = 丹麦人香烟;  
  139.                                                                                                                                                         丹麦人啊.宠物 = 丹麦人宠物;  
  140.                                                                                                                                                         丹麦人啊.房子次序 = 丹麦人房子次序;  
  141.   
  142.                                                                                                                                                         Man 挪威人啊 = new Man();  
  143.                                                                                                                                                         挪威人啊.国籍 = "挪威";  
  144.                                                                                                                                                         挪威人啊.房子次序 = 1;  
  145.                                                                                                                                                         挪威人啊.房子 = 挪威人房子;  
  146.                                                                                                                                                         挪威人啊.香烟 = 挪威人香烟;  
  147.                                                                                                                                                         挪威人啊.宠物 = 挪威人宠物;  
  148.                                                                                                                                                         挪威人啊.饮料 = 挪威人饮料;  
  149.   
  150.                                                                                                                                                         Man 德国人啊 = new Man();  
  151.                                                                                                                                                         德国人啊.国籍 = "德国";  
  152.                                                                                                                                                         德国人啊.香烟 = "PRINCE";  
  153.                                                                                                                                                         德国人啊.房子 = 德国人房子;  
  154.                                                                                                                                                         德国人啊.宠物 = 德国人宠物;  
  155.                                                                                                                                                         德国人啊.房子次序 = 德国人房子次序;  
  156.                                                                                                                                                         德国人啊.饮料 = 德国人饮料;  
  157.   
  158.                                                                                                                                                         ManContainer 老外合集 = new ManContainer();  
  159.                                                                                                                                                         老外合集.丹麦人 = 丹麦人啊;  
  160.                                                                                                                                                         老外合集.德国人 = 德国人啊;  
  161.                                                                                                                                                         老外合集.挪威人 = 挪威人啊;  
  162.                                                                                                                                                         老外合集.瑞典人 = 瑞典人啊;  
  163.                                                                                                                                                         老外合集.英国人 = 英国人啊;  
  164.                                                                                                                                                         //验证对象是符合条件  
  165.                                                                                                                                                         if (Check1(老外合集) && Check2(老外合集) && Check3(老外合集) && Check4(老外合集) && Check5(老外合集) && Check6(老外合集) && Check7(老外合集) && Check8(老外合集) && Check9(老外合集))  
  166.                                                                                                                                                         {  
  167.                                                                                                                                                             Man FuHeTiaoJian= GetManByChongWu("鱼", 老外合集);  
  168.                                                                                                                                                             Console.WriteLine("养鱼的人是:" + FuHeTiaoJian.国籍 + "人,宠物是:" + FuHeTiaoJian.宠物 + " 房子是:" + FuHeTiaoJian.房子+"色的 抽的是:"+FuHeTiaoJian.香烟+"牌香烟 喝的是:"+FuHeTiaoJian.饮料+" 房子次序是:"+FuHeTiaoJian.房子次序);  
  169.                                                                                                                                                         }  
  170.                                                                                                                                                         #endregion  
  171.                                                                                                                                                     }  
  172.                                                                                                                                                 }  
  173.                                                                                                                                             }  
  174.                                                                                                                                         }  
  175.                                                                                                                                     }  
  176.                                                                                                                                 }  
  177.                                                                                                                             }  
  178.                                                                                                                         }  
  179.                                                                                                                     }  
  180.                                                                                                                 }  
  181.                                                                                                             }  
  182.                                                                                                         }  
  183.                                                                                                     }  
  184.                                                                                                 }  
  185.                                                                                             }  
  186.                                                                                         }  
  187.                                                                                     }  
  188.                                                                                 }  
  189.                                                                             }  
  190.                                                                         }  
  191.                                                                     }  
  192.                                                                 }  
  193.                                                             }  
  194.                                                         }  
  195.                                                     }  
  196.                                                 }  
  197.                                             }  
  198.                                         }  
  199.                                     }  
  200.                                 }  
  201.                             }  
  202.                         }  
  203.                     }  
  204.                 }  
  205.             }  
  206.             #endregion  
  207.   
  208.             System.Console.Write("执行完毕!");  
  209.             System.Console.Read();  
  210.         }  
  211.  
  212.         #region 判决方法  
  213.         /// <summary>  
  214.         /// 绿房子在白房子旁边,绿房子主人喝咖啡  
  215.         /// </summary>  
  216.         /// <param name="老外合集"></param>  
  217.         /// <returns></returns>  
  218.         static public bool Check1(ManContainer 一个老外)  
  219.         {  
  220.             Man temp1 = GetManByFangZi("绿", 一个老外);  
  221.             Man temp2 = GetManByFangZi("白", 一个老外);  
  222.   
  223.             return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1 && temp1.饮料 == "咖啡");  
  224.         }  
  225.         /// <summary>  
  226.         /// 抽"PALL MALL"烟的人养 "鸟";  
  227.         /// </summary>  
  228.         /// <param name="老外合集"></param>  
  229.         /// <returns></returns>  
  230.         static public bool Check2(ManContainer 一个老外)  
  231.         {  
  232.             Man temp1 = GetManByYan("PALL MALL", 一个老外);  
  233.             return (temp1.宠物=="鸟");  
  234.         }  
  235.         /// <summary>  
  236.         /// 黄房子 主人抽 "DUNHILL"烟;  
  237.         /// </summary>  
  238.         /// <param name="老外合集"></param>  
  239.         /// <returns></returns>  
  240.         static public bool Check3(ManContainer 一个老外)  
  241.         {  
  242.             Man temp1 = GetManByFangZi("黄", 一个老外);  
  243.             return (temp1.香烟 == "DUNHILL");  
  244.         }  
  245.         /// <summary>  
  246.         /// 未知4.饮料 = "牛奶";未知4.房子次序 = 3; 中间房子的人喝牛奶  
  247.         /// </summary>  
  248.         /// <param name="老外合集"></param>  
  249.         /// <returns></returns>  
  250.         static public bool Check4(ManContainer 一个老外)  
  251.         {  
  252.             Man temp1 = GetManByHouseIndex(3, 一个老外);  
  253.             return (temp1.饮料 == "牛奶");  
  254.         }  
  255.         /// <summary>  
  256.         /// 未知5 抽混合烟的人住在样猫的人旁边!  
  257.         /// </summary>  
  258.         /// <param name="老外合集"></param>  
  259.         /// <returns></returns>  
  260.         static public bool Check5(ManContainer 一个老外)  
  261.         {  
  262.             Man temp1 = GetManByYan("混合烟", 一个老外);  
  263.             Man temp2 = GetManByChongWu("猫", 一个老外);  
  264.             return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);  
  265.         }  
  266.         /// <summary>  
  267.         /// 养马的人住在抽DUNHILL烟的旁边  
  268.         /// </summary>  
  269.         /// <param name="老外合集"></param>  
  270.         /// <returns></returns>  
  271.         static public bool Check6(ManContainer 一个老外)  
  272.         {  
  273.             Man temp1 = GetManByChongWu("马", 一个老外);  
  274.             Man temp2 = GetManByYan("DUNHILL", 一个老外);  
  275.             return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);  
  276.         }  
  277.         /// <summary>  
  278.         /// 抽BLUEMASTER烟的人喝啤酒  
  279.         /// </summary>  
  280.         /// <param name="老外合集"></param>  
  281.         /// <returns></returns>  
  282.         static public bool Check7(ManContainer 一个老外)  
  283.         {  
  284.             Man temp1 = GetManByYan("BLUE MASTER", 一个老外);  
  285.             return (temp1.饮料 == "啤酒");  
  286.         }  
  287.         /// <summary>  
  288.         /// 挪威人住在蓝房子旁边则 蓝房子 位置为2  
  289.         /// </summary>  
  290.         /// <param name="老外合集"></param>  
  291.         /// <returns></returns>  
  292.         static public bool Check8(ManContainer 一个老外)  
  293.         {  
  294.             Man temp1 = GetManByFangZi("蓝", 一个老外);  
  295.             return (temp1.房子次序 == 2);  
  296.         }  
  297.         /// <summary>  
  298.         /// 混合烟的邻居喝矿泉水  
  299.         /// </summary>  
  300.         /// <param name="老外合集"></param>  
  301.         /// <returns></returns>  
  302.         static public bool Check9(ManContainer 一个老外)  
  303.         {  
  304.             Man temp1 = GetManByYan("混合烟", 一个老外);  
  305.             Man temp2 = GetManByYingLiao("矿泉水", 一个老外);  
  306.             return (Math.Abs(temp1.房子次序 - temp2.房子次序) == 1);  
  307.         }  
  308.         #endregion  
  309.  
  310.         #region 通过一定条件获取人员  
  311.         /// <summary>  
  312.         /// 通过房子序号获取房主  
  313.         /// </summary>  
  314.         /// <param name="index"></param>  
  315.         /// <param name="一个老外"></param>  
  316.         /// <returns></returns>  
  317.         static public Man GetManByHouseIndex(int index, ManContainer 一个老外)  
  318.         {  
  319.             if (一个老外.丹麦人.房子次序 == index)  
  320.             {  
  321.                 return 一个老外.丹麦人;  
  322.             }  
  323.             if (一个老外.德国人.房子次序 == index)  
  324.             {  
  325.                 return 一个老外.德国人;  
  326.             }  
  327.             if (一个老外.挪威人.房子次序 == index)  
  328.             {  
  329.                 return 一个老外.挪威人;  
  330.             }  
  331.             if (一个老外.瑞典人.房子次序 == index)  
  332.             {  
  333.                 return 一个老外.瑞典人;  
  334.             }  
  335.             if (一个老外.英国人.房子次序 == index)  
  336.             {  
  337.                 return 一个老外.英国人;  
  338.             }  
  339.             return null;  
  340.         }  
  341.   
  342.         /// <summary>  
  343.         /// 抽某种烟的人  
  344.         /// </summary>  
  345.         /// <param name="index"></param>  
  346.         /// <param name="一个老外"></param>  
  347.         /// <returns></returns>  
  348.         static public Man GetManByYan(string 烟名, ManContainer 一个老外)  
  349.         {  
  350.             if (一个老外.丹麦人.香烟 == 烟名)  
  351.             {  
  352.                 return 一个老外.丹麦人;  
  353.             }  
  354.             if (一个老外.德国人.香烟 == 烟名)  
  355.             {  
  356.                 return 一个老外.德国人;  
  357.             }  
  358.             if (一个老外.挪威人.香烟 == 烟名)  
  359.             {  
  360.                 return 一个老外.挪威人;  
  361.             }  
  362.             if (一个老外.瑞典人.香烟 == 烟名)  
  363.             {  
  364.                 return 一个老外.瑞典人;  
  365.             }  
  366.             if (一个老外.英国人.香烟 == 烟名)  
  367.             {  
  368.                 return 一个老外.英国人;  
  369.             }  
  370.             return null;  
  371.         }  
  372.   
  373.         /// <summary>  
  374.         /// 养某种宠物  
  375.         /// </summary>  
  376.         /// <param name="index"></param>  
  377.         /// <param name="一个老外"></param>  
  378.         /// <returns></returns>  
  379.         static public Man GetManByChongWu(string 宠物名, ManContainer 一个老外)  
  380.         {  
  381.             if (一个老外.丹麦人.宠物 == 宠物名)  
  382.             {  
  383.                 return 一个老外.丹麦人;  
  384.             }  
  385.             if (一个老外.德国人.宠物 == 宠物名)  
  386.             {  
  387.                 return 一个老外.德国人;  
  388.             }  
  389.             if (一个老外.挪威人.宠物 == 宠物名)  
  390.             {  
  391.                 return 一个老外.挪威人;  
  392.             }  
  393.             if (一个老外.瑞典人.宠物 == 宠物名)  
  394.             {  
  395.                 return 一个老外.瑞典人;  
  396.             }  
  397.             if (一个老外.英国人.宠物 == 宠物名)  
  398.             {  
  399.                 return 一个老外.英国人;  
  400.             }  
  401.             return null;  
  402.         }  
  403.   
  404.         /// <summary>  
  405.         /// 住某种颜色房子的  
  406.         /// </summary>  
  407.         /// <param name="index"></param>  
  408.         /// <param name="一个老外"></param>  
  409.         /// <returns></returns>  
  410.         static public Man GetManByFangZi(string 房子颜色, ManContainer 一个老外)  
  411.         {  
  412.             if (一个老外.丹麦人.房子 == 房子颜色)  
  413.             {  
  414.                 return 一个老外.丹麦人;  
  415.             }  
  416.             if (一个老外.德国人.房子 == 房子颜色)  
  417.             {  
  418.                 return 一个老外.德国人;  
  419.             }  
  420.             if (一个老外.挪威人.房子 == 房子颜色)  
  421.             {  
  422.                 return 一个老外.挪威人;  
  423.             }  
  424.             if (一个老外.瑞典人.房子 == 房子颜色)  
  425.             {  
  426.                 return 一个老外.瑞典人;  
  427.             }  
  428.             if (一个老外.英国人.房子 == 房子颜色)  
  429.             {  
  430.                 return 一个老外.英国人;  
  431.             }  
  432.             return null;  
  433.         }  
  434.   
  435.         /// <summary>  
  436.         /// 喝某种饮料的人  
  437.         /// </summary>  
  438.         /// <param name="index"></param>  
  439.         /// <param name="一个老外"></param>  
  440.         /// <returns></returns>  
  441.         static public Man GetManByYingLiao(string 饮料名称, ManContainer 一个老外)  
  442.         {  
  443.             if (一个老外.丹麦人.饮料 == 饮料名称)  
  444.             {  
  445.                 return 一个老外.丹麦人;  
  446.             }  
  447.             if (一个老外.德国人.饮料 == 饮料名称)  
  448.             {  
  449.                 return 一个老外.德国人;  
  450.             }  
  451.             if (一个老外.挪威人.饮料 == 饮料名称)  
  452.             {  
  453.                 return 一个老外.挪威人;  
  454.             }  
  455.             if (一个老外.瑞典人.饮料 == 饮料名称)  
  456.             {  
  457.                 return 一个老外.瑞典人;  
  458.             }  
  459.             if (一个老外.英国人.饮料 == 饮料名称)  
  460.             {  
  461.                 return 一个老外.英国人;  
  462.             }  
  463.             return null;  
  464.         }  
  465.         #endregion  
  466.     }  
  467.  
  468.     #region 对象模型  
  469.     //人员模型  
  470.     public class Man:Object  
  471.     {  
  472.         #region 属性  
  473.         private string guoji="";  
  474.         public string 国籍  
  475.         {  
  476.             get  
  477.             {  
  478.                 return guoji;  
  479.             }  
  480.             set  
  481.             {  
  482.                 guoji = value;  
  483.             }  
  484.         }  
  485.   
  486.   
  487.         private string yinliao = "";  
  488.         public string 饮料  
  489.         {  
  490.             get  
  491.             {  
  492.                 return yinliao;  
  493.             }  
  494.             set  
  495.             {  
  496.                 yinliao = value;  
  497.             }  
  498.         }  
  499.   
  500.   
  501.         private string xiangyan = "";  
  502.         public string 香烟  
  503.         {  
  504.             get  
  505.             {  
  506.                 return xiangyan;  
  507.             }  
  508.             set  
  509.             {  
  510.                 xiangyan = value;  
  511.             }  
  512.         }  
  513.   
  514.         private string chongwu = "";  
  515.         public string 宠物  
  516.         {  
  517.             get  
  518.             {  
  519.                 return chongwu;  
  520.             }  
  521.             set  
  522.             {  
  523.                 chongwu = value;  
  524.             }  
  525.         }  
  526.   
  527.         private string fangzi = "";  
  528.         public string 房子  
  529.         {  
  530.             get  
  531.             {  
  532.                 return fangzi;  
  533.             }  
  534.             set  
  535.             {  
  536.                 fangzi = value;  
  537.             }  
  538.         }  
  539.   
  540.         private int fangziindex=0;  
  541.         public int 房子次序  
  542.         {  
  543.             get  
  544.             {  
  545.                 return fangziindex;  
  546.             }  
  547.             set  
  548.             {  
  549.                 fangziindex = value;  
  550.             }  
  551.         }  
  552.         #endregion  
  553.         #region 方法  
  554.         public override bool Equals(object obj)  
  555.         {  
  556.             return base.Equals(obj);  
  557.         }  
  558.         public void Copy(Man a)  
  559.         {  
  560.             this.国籍 = a.国籍;  
  561.             this.香烟 = a.香烟;  
  562.             this.饮料 = a.饮料;  
  563.             this.宠物 = a.宠物;  
  564.             this.房子 = a.房子;  
  565.         }  
  566.         #endregion  
  567.     }  
  568.     //业务模型,为结果的一种可能性  
  569.     public class ManContainer  
  570.     {  
  571.         public Man 英国人;  
  572.         public Man 瑞典人;  
  573.         public Man 丹麦人;  
  574.         public Man 挪威人;  
  575.         public Man 德国人;  
  576.     }  
  577.     #endregion  
  578. }  



算法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

有钱的朋友上我的CSDN去奉献点分吧,极品资源很多哦!:http://download.csdn.net/user/devgis
原文地址:https://www.cnblogs.com/devgis/p/2772967.html