Created
September 12, 2012 13:53
-
-
Save logicmd/3706748 to your computer and use it in GitHub Desktop.
有两堆硬币,一堆有27个,一堆有26个,分别抛这两堆硬币,哪堆的正面朝上的硬币数量多,哪堆算赢。问,27个硬币的那堆赢的概率有多少?输的概率有多少
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
% 有两堆硬币,一堆有27个,一堆有26个,分别抛这两堆硬币, % | |
% 哪堆的正面朝上的硬币数量多,哪堆算赢。问,27个硬币的那 % | |
% 堆赢的概率有多少?输的概率有多少? % | |
% % | |
% % | |
% p_win = 0.5000, p_lose = 0.3919 % | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
a_num = 27; | |
b_num = 26; | |
p_a = zeros(1, a_num + 1); | |
p_b = zeros(1, b_num + 1); | |
for i=1:(a_num + 1) | |
p_a(i) = nchoosek(a_num, i-1) * (1/2)^a_num; | |
if(i > (b_num + 1)) | |
break; | |
end | |
p_b(i) = nchoosek(b_num, i-1) * (1/2)^b_num; | |
end | |
if (sum(p_a) ~=1 || sum(p_b)~=1) | |
error('fucked in early stage'); | |
end | |
p_win = 0; | |
p_lose = 0; | |
p_equal = 0; | |
for i=1:(a_num + 1) | |
for j=1:(b_num + 1) | |
if (i > j) | |
p_win = p_win + p_a(i) * p_b(j); | |
end | |
if (i < j) | |
p_lose = p_lose + p_a(i) * p_b(j); | |
end | |
if (i == j) | |
p_equal = p_equal + p_a(i) * p_b(j); | |
end | |
end | |
end | |
if (abs(p_win + p_lose + p_equal - 1) > 1e-4) | |
error('checked failed'); | |
end | |
p_win | |
p_lose | |
p_equal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment