我不知道 Python,但我无法抗拒在 Ruby 中尝试它。希望这将很容易地转换为 Python。如果你不了解 Ruby,我很乐意解释这里发生了什么:
num_players = gets.to_i
players = (1..num_players).to_a
teams = players.combination(2).to_a
def shuffle_teams( teams, players )
shuffled_teams = teams.shuffle
x = 0
while x < shuffled_teams.length
if shuffled_teams[x] - shuffled_teams[x + 1] == shuffled_teams[x]
x += 2
else
return shuffle_teams( teams, players )
end
end
x = 0
while x < shuffled_teams.length
team_1 = shuffled_teams[x]
team_2 = shuffled_teams[x + 1]
waiting = players.select do |player|
![team_1, team_2].flatten.include?(player)
end
print "(#{team_1}, #{team_2}), waiting: #{waiting}\n"
x += 2
end
end
shuffle_teams( teams, players )
这将为 4 个玩家产生正确的输出:
([3, 4], [1, 2]), waiting: []
([1, 3], [2, 4]), waiting: []
([2, 3], [1, 4]), waiting: []
对于 5 名玩家:
([2, 4], [1, 3]), waiting: [5]
([1, 5], [3, 4]), waiting: [2]
([1, 4], [2, 5]), waiting: [3]
([3, 5], [1, 2]), waiting: [4]
([2, 3], [4, 5]), waiting: [1]
但是,它不适用于 6 或 7 名玩家,因为每个玩家都会产生奇数个组合。这个问题在现实生活中是如何处理的?不知何故,一支球队将不得不打两次。
编辑:此脚本现在将通过复制其中一个团队来处理 6 或 7 个玩家池。它应该很容易在 Python 中复制,因为它只依赖于对团队数组进行洗牌,直到他们进入适当的顺序。起初,我觉得我用这种方法有点作弊,但鉴于 Anonymous 解释这是一个 NP 完全问题(假设我正确理解这意味着什么:-),这可能是解决问题的最佳方法小池(它会因池大于 9 左右而爆炸,具体取决于您的系统,但幸运的是,这超出了我们的场景范围)。再加上随机排序的优点是没有人情味,如果玩家因为必须玩两次而没有第二次得分而感到不安,这可能会派上用场!这是脚本:
num_players = gets.to_i
players = (1..num_players).to_a
teams = players.combination(2).to_a
def shuffle_teams( teams, players )
shuffled_teams = teams.shuffle
x = 0
while x < shuffled_teams.length
if !shuffled_teams[x + 1]
shuffled_teams[x + 1] = shuffled_teams.find do |team|
shuffled_teams[x] - team == shuffled_teams[x]
end
end
if shuffled_teams[x] - shuffled_teams[x + 1] == shuffled_teams[x]
x += 2
else
return shuffle_teams( teams, players )
end
end
x = 0
while x < shuffled_teams.length
team_1 = shuffled_teams[x]
team_2 = shuffled_teams[x + 1]
waiting = players.select do |player|
![team_1, team_2].flatten.include?(player)
end
print "(#{team_1}, #{team_2}), waiting: #{waiting}\n"
x += 2
end
end
shuffle_teams( teams, players )
这是输出,有时间:
4
([1, 4], [2, 3]), waiting: []
([1, 2], [3, 4]), waiting: []
([2, 4], [1, 3]), waiting: []
real 0m0.293s
user 0m0.035s
sys 0m0.015s
5
([4, 5], [1, 2]), waiting: [3]
([1, 4], [2, 3]), waiting: [5]
([2, 5], [1, 3]), waiting: [4]
([2, 4], [3, 5]), waiting: [1]
([3, 4], [1, 5]), waiting: [2]
real 0m0.346s
user 0m0.040s
sys 0m0.010s
6
([3, 4], [1, 2]), waiting: [5, 6]
([3, 5], [2, 4]), waiting: [1, 6]
([3, 6], [1, 5]), waiting: [2, 4]
([1, 6], [2, 5]), waiting: [3, 4]
([2, 3], [4, 6]), waiting: [1, 5]
([2, 6], [4, 5]), waiting: [1, 3]
([5, 6], [1, 4]), waiting: [2, 3]
([1, 3], [2, 4]), waiting: [5, 6]
real 0m0.348s
user 0m0.035s
sys 0m0.020s
7
([1, 6], [4, 5]), waiting: [2, 3, 7]
([2, 6], [1, 4]), waiting: [3, 5, 7]
([2, 7], [1, 3]), waiting: [4, 5, 6]
([3, 4], [2, 5]), waiting: [1, 6, 7]
([3, 5], [2, 4]), waiting: [1, 6, 7]
([1, 7], [5, 6]), waiting: [2, 3, 4]
([6, 7], [1, 5]), waiting: [2, 3, 4]
([3, 6], [4, 7]), waiting: [1, 2, 5]
([1, 2], [5, 7]), waiting: [3, 4, 6]
([3, 7], [4, 6]), waiting: [1, 2, 5]
([2, 3], [1, 6]), waiting: [4, 5, 7]
real 0m0.332s
user 0m0.050s
sys 0m0.010s