小编inc*_*ick的帖子

排球运动员组合

一些背景:
在排球中,球员在水池中进行比赛以确定排名.球队是一对球员.比赛是一对球员与另一对球员.对于这个例子,我们假设只有一个球场可以进行比赛,当球员没有比赛时,他们坐着/等待.池中玩家的数量将在4到7之间.如果池中有8个玩家,他们只会将其分成2个4个池.

我想计算最少的比赛数量,以便每个球员与其他球员一起比赛.

例如,一个4人游戏池将拥有以下团队:

import itertools
players = [1,2,3,4]
teams = [t for t in itertools.combinations(players,2)]
print 'teams:'
for t in teams:
    print t
Run Code Online (Sandbox Code Playgroud)

输出:

teams:
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
Run Code Online (Sandbox Code Playgroud)

和比赛数量:

matches = []
for match in itertools.combinations(teams,2):
    # A player cannot be on both teams at the same time
    if set(match[0]) & set(match[1]) == set():
        matches.append(match)

for match in matches:
    print match
Run Code Online (Sandbox Code Playgroud)

输出:

((1, 2), (3, 4))
((1, 3), (2, 4)) …
Run Code Online (Sandbox Code Playgroud)

algorithm combinations

13
推荐指数
1
解决办法
1771
查看次数

标签 统计

algorithm ×1

combinations ×1