The comment toutiao was posted in yesterday's article "Interview with an interview question". The address is http://www.cnblogs.com/haolujun/archive/2012/10/20/273%3.html.
This interview question is a classic data probability problem. Since it is a programmer, of course there must be a programmer's solution: Use a program to describe the business, and then use a computer to calculate the desired result.
The Code is as follows:
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 ("Obama", ""); dcPeople. add ("Romney", ""); Dictionary <string, string> dcBox = new Dictionary <string, string> (); dcBox. add ("A", "shit"); dcBox. add ("B", "shit"); dcBox. add ("C", "shit"); 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); // for the first time, Obama chooses int choose_aobama_int = r. next (1, 4); switch (choose_aobama_int) {case 1: dcPeople ["Obama"] = "A"; break; case 2: dcPeople ["Obama"] = "B"; break; case 3: dcPeople ["Obama"] = "C"; break; default: break;} string theShit = ""; // It's definitely a shit box. // Mr. Obama chooses to remove a shit. There are two cases: first, Obama didn't guess the shit. Second: step on the // Obama step on the shit if (dcBox [dcPeople ["Obama"] = "shit") {foreach (var item in dcBox) {if (item. value = "shit") {theShit = item. key ;}} else {List <string> abcBox = new List <string> (); abcBox. add ("A"); abcBox. add ("B"); abcBox. add ("C"); abcBox. remove (dcPeople ["Obama"]); // randomly select one of the remaining two boxes marked as shit int shit_int = r. next (0, 2); theShit = abcBox [shit_int];} // Next, select if (dcBox [dcPeople ["Obama"] = "shit ") // if Obama chooses a shit, he will surely know which one is the treasure, and he will certainly be able to choose the right one. {foreach (var item in dcBox) {if (item. value = "treasure") {dcPeople ["Romney"] = item. key ;}} else // if Obama selects the treasure, he can only randomly select {int choose_luomuni_int = r in the remaining two. next (0, 2); List <string> abcBox = new List <string> (); abcBox. add ("A"); abcBox. add ("B"); abcBox. add ("C"); abcBox. remove (dcPeople ["Obama"]); dcPeople ["Romney"] = abcBox [choose_luomuni_int];} Console. writeLine (); Console. writeLine ("----------------------- results of this round of lottery -------------------------"); Console. writeLine ("Obama: To [" + dcBox [dcPeople ["Obama"] + "]"); Console. writeLine ("Romney: To [" + dcBox [dcPeople ["Romney"] + "]"); if (dcBox [dcPeople ["Obama"] = "") {aobamaWinCount = aobamaWinCount + 1;} else {luomuniWinCount = luomuniWinCount + 1;} count ++; if (count == 900) {Console. writeLine ("game ended"); Console. writeLine ("the game has been performed" + count + "times"); Console. writeLine ("Obama pulled treasures" + aobamaWinCount + "times"); Console. writeLine ("Mr. Romney pulled the treasure" + luomuniWinCount + "times"); Console. read (); break ;}}}}}
You can create a new console program, copy the code, and press F5. wait patiently for 45 seconds and you will be notified of the result.
The code is written on a temporary basis, including naming or some shortcomings.