程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#用兩種方式破解號稱世界上最難的問題!愛因斯坦在20世紀初出的謎語

C#用兩種方式破解號稱世界上最難的問題!愛因斯坦在20世紀初出的謎語

編輯: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香煙的人有一個喝水的鄰居
你們可以猜得出來嗎?


算法1:

[csharp] 
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;

//   }

 

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved