條件機率(終極版) - 拼圖

By Michael
at 2009-11-24T00:18
at 2009-11-24T00:18
Table of Contents
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<ctime>
5
6 using namespace std;
7
8 int o[2] = {0} ;
9
10 int Rand()
11 {
12 int x = rand() % 2;
13 o[x] ++ ;
14 return x ;
15 }
16
17 int main()
18 {
19 srand( time(NULL) ) ;
20
21 int n , B ;
22
23 cout << "How many times you want to play?" << endl ;
24 cin >> n ; //模擬 n 次
25 cout << "How many balls ?" << endl ;
26 cin >> B ; //袋子裡有B顆球
27
28 int b = 0 , w = 0 ;
29 for( int i = 0 ; i < n ; )
30 {
31 int color[10] ;
32
33 for( int k = 0 ; k < B ; k ++ )
34 color[k] = Rand() ; //決定球的顏色
35 int c = 0 ;
36
37 for( int k = 0 ; k < B ; k ++ )
38 if( color[k] )
39 c ++ ; //計算袋子裡有幾顆白球
40
41 if( c >= B - 1 ) //如果袋子裡至少有B-1顆球
42 {
43 if( c == B ) //如果剩下的那一顆也是白色
44 w ++ ;
45 else //不然就是黑色
46 b ++ ;
47 i ++ ;
48 }
49 }
50
51 cout << "0: " << o[0] << "\t" << 100.*o[0]/(o[0]+o[1]) << "%" << endl ;
52 cout << "1: " << o[1] << "\t" << 100.*o[1]/(o[0]+o[1]) << "%" << endl ;
53 puts( "------------------------------------" ) ;
54 cout << "Result:" << endl ;
55 cout << "W: " << w << "\t" << 100.*w/(w+b) << "%" << endl ;
56 cout << "B: " << b << "\t" << 100.*b/(w+b) << "%" << endl ;
57
58 return 0;
59 }
執行結果:(已翻譯和補充說明)
模擬幾次?
1000000
袋子裡有幾顆球?
2
------------------------------------
模擬結果:
在這次的模擬中,亂數選到0的機率是:
1333605次 50.0043%
在這次的模擬中,亂數選到1的機率是:
1333375次 49.9957%
------------------------------------
你拿到白球333375次 機率 33.3375%
你拿到黑球666625次 機率 66.6625%
模擬幾次?
1000000
袋子裡有幾顆球?
4
------------------------------------
模擬結果:
在這次的模擬中,亂數選到0的機率是:
6394380次 49.9949%
在這次的模擬中,亂數選到1的機率是:
6395688次 50.0051%
------------------------------------
你拿到白球199272次 機率 19.9272%
你拿到黑球800728次 機率 80.0728%
解釋一下:
這裡的概念是,如果袋子裡恰好有B-1個白球,那你就是會選到黑球
如果袋子裡全部是白球,那你選到白球(廢話)
袋子裡白球少於B-1個的情況不在樣本空間中。
在模擬時,亂數選到1代表白球,選到0代表黑球。
--
2 #include<cstdio>
3 #include<cstdlib>
4 #include<ctime>
5
6 using namespace std;
7
8 int o[2] = {0} ;
9
10 int Rand()
11 {
12 int x = rand() % 2;
13 o[x] ++ ;
14 return x ;
15 }
16
17 int main()
18 {
19 srand( time(NULL) ) ;
20
21 int n , B ;
22
23 cout << "How many times you want to play?" << endl ;
24 cin >> n ; //模擬 n 次
25 cout << "How many balls ?" << endl ;
26 cin >> B ; //袋子裡有B顆球
27
28 int b = 0 , w = 0 ;
29 for( int i = 0 ; i < n ; )
30 {
31 int color[10] ;
32
33 for( int k = 0 ; k < B ; k ++ )
34 color[k] = Rand() ; //決定球的顏色
35 int c = 0 ;
36
37 for( int k = 0 ; k < B ; k ++ )
38 if( color[k] )
39 c ++ ; //計算袋子裡有幾顆白球
40
41 if( c >= B - 1 ) //如果袋子裡至少有B-1顆球
42 {
43 if( c == B ) //如果剩下的那一顆也是白色
44 w ++ ;
45 else //不然就是黑色
46 b ++ ;
47 i ++ ;
48 }
49 }
50
51 cout << "0: " << o[0] << "\t" << 100.*o[0]/(o[0]+o[1]) << "%" << endl ;
52 cout << "1: " << o[1] << "\t" << 100.*o[1]/(o[0]+o[1]) << "%" << endl ;
53 puts( "------------------------------------" ) ;
54 cout << "Result:" << endl ;
55 cout << "W: " << w << "\t" << 100.*w/(w+b) << "%" << endl ;
56 cout << "B: " << b << "\t" << 100.*b/(w+b) << "%" << endl ;
57
58 return 0;
59 }
執行結果:(已翻譯和補充說明)
模擬幾次?
1000000
袋子裡有幾顆球?
2
------------------------------------
模擬結果:
在這次的模擬中,亂數選到0的機率是:
1333605次 50.0043%
在這次的模擬中,亂數選到1的機率是:
1333375次 49.9957%
------------------------------------
你拿到白球333375次 機率 33.3375%
你拿到黑球666625次 機率 66.6625%
模擬幾次?
1000000
袋子裡有幾顆球?
4
------------------------------------
模擬結果:
在這次的模擬中,亂數選到0的機率是:
6394380次 49.9949%
在這次的模擬中,亂數選到1的機率是:
6395688次 50.0051%
------------------------------------
你拿到白球199272次 機率 19.9272%
你拿到黑球800728次 機率 80.0728%
解釋一下:
這裡的概念是,如果袋子裡恰好有B-1個白球,那你就是會選到黑球
如果袋子裡全部是白球,那你選到白球(廢話)
袋子裡白球少於B-1個的情況不在樣本空間中。
在模擬時,亂數選到1代表白球,選到0代表黑球。
--
Tags:
拼圖
All Comments

By Irma
at 2009-11-27T21:47
at 2009-11-27T21:47

By Olive
at 2009-11-29T18:28
at 2009-11-29T18:28

By Rosalind
at 2009-11-29T23:26
at 2009-11-29T23:26

By Madame
at 2009-12-04T11:16
at 2009-12-04T11:16

By Tristan Cohan
at 2009-12-07T16:16
at 2009-12-07T16:16

By Joe
at 2009-12-08T15:05
at 2009-12-08T15:05

By Harry
at 2009-12-09T10:46
at 2009-12-09T10:46

By Audriana
at 2009-12-13T08:12
at 2009-12-13T08:12

By Skylar DavisLinda
at 2009-12-16T21:02
at 2009-12-16T21:02

By Donna
at 2009-12-19T02:30
at 2009-12-19T02:30

By Gary
at 2009-12-23T23:12
at 2009-12-23T23:12

By Daniel
at 2009-12-28T20:27
at 2009-12-28T20:27

By Lauren
at 2009-12-29T00:54
at 2009-12-29T00:54

By Agatha
at 2009-12-30T12:36
at 2009-12-30T12:36

By Agatha
at 2010-01-02T08:16
at 2010-01-02T08:16

By Ula
at 2010-01-04T17:15
at 2010-01-04T17:15

By Regina
at 2010-01-06T18:14
at 2010-01-06T18:14

By Charlie
at 2010-01-07T06:26
at 2010-01-07T06:26

By Lauren
at 2010-01-09T02:31
at 2010-01-09T02:31

By Skylar DavisLinda
at 2010-01-12T20:54
at 2010-01-12T20:54
Related Posts
條件機率(終極版)

By Oscar
at 2009-11-23T23:11
at 2009-11-23T23:11
條件機率(終極版)

By Callum
at 2009-11-23T22:50
at 2009-11-23T22:50
條件機率(終極版)

By Freda
at 2009-11-23T22:45
at 2009-11-23T22:45
賭局系列 PART(Ⅶ)發功搓牌

By Delia
at 2009-11-23T22:41
at 2009-11-23T22:41
條件機率(終極版)

By Susan
at 2009-11-23T22:30
at 2009-11-23T22:30