【戲談一道面試題】-----之用C#代碼解決


話說昨天一篇文章《戲談一道面試題》上了評論頭條,地址如下http://www.cnblogs.com/haolujun/archive/2012/10/20/2731903.html

該面試題是一個經典的數據概率問題,而既然是程序員,當然得有程序員的解決方案:用程序來描述業務,然后通過計算機做計算來算出所要的結果。

 

代碼如下:

View Code
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            int aobamaWinCount = 0;
            int luomuniWinCount = 0;
            while (true)
            {
                Random r = new Random(unchecked((int)(DateTime.Now.Ticks)));
            
               

                Dictionary<string, string> dcPeople = new Dictionary<string, string>();
                dcPeople.Add("奧巴馬", "");
                dcPeople.Add("羅姆尼", "");

                Dictionary<string, string> dcBox = new Dictionary<string, string>();
                dcBox.Add("A", "狗屎");
                dcBox.Add("B", "狗屎");
                dcBox.Add("C", "狗屎");


                int treasure_int = r.Next(1, 4);
                switch (treasure_int)
                {
                    case 1:
                        dcBox["A"] = "寶物";
                        break;
                    case 2:
                        dcBox["B"] = "寶物";
                        break;
                    case 3:
                        dcBox["C"] = "寶物";
                        break;
                    default:
                        break;
                }

                System.Threading.Thread.Sleep(50);



                //第一次奧巴馬選擇
                int choose_aobama_int = r.Next(1, 4);
                switch (choose_aobama_int)
                {
                    case 1:
                        dcPeople["奧巴馬"] = "A";
                        break;
                    case 2:
                        dcPeople["奧巴馬"] = "B";
                        break;
                    case 3:
                        dcPeople["奧巴馬"] = "C";
                        break;
                    default:
                        break;
                }

                

                string theShit = "";  //確定是狗屎的盒子

                //羅姆尼選擇去掉一坨狗屎,這里有兩種情況,第一,奧巴馬沒猜到狗屎   第二:踩到了

                //奧巴馬踩到狗屎了
                if (dcBox[dcPeople["奧巴馬"]] == "狗屎")
                {
                    foreach (var item in dcBox)
                    {
                        if (item.Value == "狗屎")
                        {
                            theShit = item.Key;
                        }
                    }
                }
                else
                {
                    List<string> abcBox = new List<string>();
                    abcBox.Add("A");
                    abcBox.Add("B");
                    abcBox.Add("C");
                    abcBox.Remove(dcPeople["奧巴馬"]);

                    //在剩下的兩個盒子中隨機選擇一個標記為狗屎
                    int shit_int = r.Next(0, 2);

                    
                    
                    theShit = abcBox[shit_int];
                }



                //接下來是羅姆尼選擇
                if (dcBox[dcPeople["奧巴馬"]] == "狗屎")//如果奧巴馬選擇了狗屎,那么羅姆尼肯定知道剩下的哪個是寶物,肯定能選對寶物
                {
                    foreach (var item in dcBox)
                    {
                        if (item.Value == "寶物")
                        {
                            dcPeople["羅姆尼"] = item.Key;
                        }
                    }
                }
                else//如果奧巴馬選中了寶物,那羅姆尼就只能在剩下的兩個隨機選取了
                {
                    int choose_luomuni_int = r.Next(0, 2);

                    

                    List<string> abcBox = new List<string>();
                    abcBox.Add("A");
                    abcBox.Add("B");
                    abcBox.Add("C");
                    abcBox.Remove(dcPeople["奧巴馬"]);
                    dcPeople["羅姆尼"] = abcBox[choose_luomuni_int];
                }

                Console.WriteLine();
                Console.WriteLine("-----------------------本輪抽獎結果---------------------------");
                Console.WriteLine("奧巴馬:抽到【" + dcBox[dcPeople["奧巴馬"]] + "");
                Console.WriteLine("羅姆尼:抽到【" + dcBox[dcPeople["羅姆尼"]] + "");


                if (dcBox[dcPeople["奧巴馬"]] == "寶物")
                {
                    aobamaWinCount = aobamaWinCount + 1;
                }
                else
                {
                    luomuniWinCount = luomuniWinCount + 1;
                }

                count++;
                if (count == 900)
                {
                    Console.WriteLine("游戲結束");
                    Console.WriteLine("游戲一共進行了"+count+"");
                    Console.WriteLine("奧巴馬抽到寶物" + aobamaWinCount + "");
                    Console.WriteLine("羅姆尼抽到寶物" + luomuniWinCount + "");

                    Console.Read();
                    break;
                }
                
            }

        }
    }
}

各位新建一個控制台程序,然后復制代碼過去,按F5,耐心等候45秒,就會告訴你結果。

代碼是臨時寫的,包括命名或者某些方面不足敬請諒解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM