问题描述
我的问题是:我有12名球员,其中3名分别被命名为A,B和C。 12名玩家被分成2组,每组6人。我需要计算出球员A和B在同一支球队中,而球员C在相反的一支球队中的可能性。数学不是我的强项,因为我很确定这不是一件难事,但是如果您能帮助我,我将不胜感激。这是我到目前为止写的:
import random
playersnumb = 12
players = list(range(12))
A = random.choice([x for x in range(12)])
B = random.choice([x for x in range(12) if x != A])
C = random.choice([x for x in range(12) if (x != A) and (x != B)])
random.shuffle(players)
team1 = (players[:6])
team2 = (players[6:])
if A in team1:
print("Player A is in team 1")
else:
print("Player A is in team 2")
if B in team1:
print("Player B is in team 1")
else:
print("Player B is in team 2")
if C in team1:
print("Player C is in team 1")
else:
print("Player C is in team 2")
感谢您的帮助。
解决方法
填写1个列表(共六个)的方式数= 12!/(6!* 6!) foreach($value as $a)
{
$array = $a->VALUE;
$ar = unserialize($array);
echo $ar[0];
echo '</br>';
}
填充六个列表(包括A和B而不是C)的方式的数量= 9!/(4!* 5!)comb(12,6)
还要查找(不是A,不是B和C)= 9!/(5!* 4!)comb(9,4)
comb(9,5)
,
我根据您的代码写了一点。这个想法是要遍历您的测试代码多次,它不是100%准确,但是我认为对您来说足够了:
import random
def calculate(playercount: int = 12) -> bool:
players = list(range(playercount))
player_a = random.choice([x for x in range(playercount)])
player_b = random.choice([x for x in range(playercount) if x != player_a])
player_c = random.choice([x for x in range(playercount) if (x != player_a) and (x != player_b)])
random.shuffle(players)
team1 = (players[:playercount//2])
team2 = (players[playercount//2:])
# That are your "positive" events
return (player_a in team1 and player_b in team1 and player_c in team2) or\
(player_a in team2 and player_b in team2 and player_c in team1)
def calculate_all(runtimes: int = 100000) -> float:
counter = 0
# count all poyitive events
for i in range(runtimes):
if calculate():
counter += 1
# return how often they appeared,based on all tests
return counter / runtimes
print("The probability is about {} %".format(calculate_all() * 100))