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

來源:互聯網
上載者:User

話說昨天一篇文章《戲談一道面試題》上了評論頭條,地址如下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秒,就會告訴你結果。

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.