马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
具体的问题可以看这里:
http://zh.wikipedia.org/w/index. ... }&variant=zh-cn
电影介绍看这里:
http://zh.wikipedia.org/wiki/決勝21點
我很早之前就听过这个问题。几年前,不知道何故,问过一个朋友,
那个朋友几秒钟后给我的答案是应该再选一次。虽然我的朋友是一个数学天才(被废了的那种),
但我一直不服气。前些时候,看了一个解释,看的时候觉得有点道理;今早起来,又觉是好象不对。
想了一下,为何我不写个程序去模拟一下呢?
于是我写了下面的程序,运行结果是:- $ java LotoTest 99999999
- PickHit = 33322046, Other = 66677953, Total = 99999999
复制代码 其实,写程序的过程中我已经知道问题在哪里了。对照core同学那个“动手”转载,的确,又一次
证实我们的,至少是小弟本人的大脑并没有进化到思考现代问题的程度。
下面是程序,注释写得很烂,将就些看吧:- 1 import java.util.*;
- 2 import static java.lang.System.out;
- 3
- 4 public class LotoTest {
- 5 private static final int PARAM_NUM = 1;
- 6
- 7 private static void usage(){
- 8 out.println("Usage: java -cp <classpath> LotoTest <num>\n");
- 9 }
- 10
- 11 /*
- 12 * Algorithm:
- 13 * Giving an Array of 3 boolean, one and only one
- 14 * of them are true. Randomly pick up a value A[x].
- 15 * then the program unveal another one which is not
- 16 * true. Run the program to determine the possibility
- 17 * that A[x] is true and the possibility that A[rest]
- 18 * is true.
- 19 */
- 20 public static void main(String [] args){
- 21 if(args.length != PARAM_NUM) {
- 22 usage();
- 23 return;
- 24 }
- 25
- 26 int runNum = Integer.valueOf(args[0]);
- 27
- 28 //Create three randomizer
- 29 //One for value setting
- 30 //One for picking up value
- 31 //The other for unveal
- 32 Random ran01 = new Random(new Date().getTime());
- 33 Random ran02 = new Random(
- 34 LotoTest.class.hashCode()
- 35 * ran01.nextLong() * new Date().getTime());
- 36 Random ran03 = new Random(
- 37 LotoTest.class.hashCode()
- 38 * ran02.nextLong()
- 39 * ran01.nextLong() * new Date().getTime());
- 40
- 41 int pickHit = 0;
- 42
- 43 for(int runc=0; runc<runNum; ++runc) {
- 44 //Generate the position to store the true value
- 45 int pos = (int)(ran01.nextDouble() * 3);
- 46 //declare and initialize the boolean array to false
- 47 boolean A[] = new boolean[3];
- 48
- 49 A[pos] = true;
- 50
- 51 //pick up one position for lucky
- 52 int pickup = (int)(ran02.nextDouble() * 3);
- 53
- 54 //unveal one of the false position
- 55 int unveal = 0;
- 56
- 57 do{
- 58 unveal = (int)(ran03.nextDouble() * 3);
- 59 if(unveal == pickup)
- 60 continue;
- 61 if(unveal == pos)
- 62 continue;
- 63 break;
- 64 }while(true);
- 65
- 66 if(pickup == pos)
- 67 ++ pickHit;
- 68
- 69 }
- 70
- 71 out.printf("PickHit = %d, Other = %d, Total = %d\n",
- 72 pickHit, runNum - pickHit, runNum);
- 73 }
- 74 }
复制代码 最后,谨借本文,遥祝那位智商暴棚,但说话不大灵光的朋友一生幸福。 |