algorithm - 从列表中的元素创建所有可能的对,然后对它们进行均匀排序

标签 algorithm sorting matching

所以我有 3 到 8 个玩家,我希望每个人都与其他人进行 1 对 1 的比赛。 比赛按顺序进行,一次一场。 一对中的顺序并不重要(a vs b == b vs a)。

我在一个简单的嵌套循环中创建对战对,在这个循环中我遍历玩家并将当前索引与以下所有索引配对。

问题是玩家 1 将完成所有比赛,然后无事可做,而玩家 8 则必须等待。

有没有什么办法可以平均分配对数,让所有玩家的停机时间均等?

我目前正在随机化包含对的数组,但我正在寻找更好的方法。

最佳答案

您可以使用 Round-robin tournament scheduling algorithm .

The circle method is the standard algorithm to create a schedule for a round-robin tournament. All competitors are assigned to numbers, and then paired in the first round:

Round 1. (1 plays 14, 2 plays 13, ... )

 1  2  3  4  5  6  7
14 13 12 11 10  9  8    

then fix one of the contributors in the first or last column of the table (number one in this example) and rotate the others clockwise one position

Round 2. (1 plays 13, 14 plays 12, ... )

 1 14  2  3  4  5  6
13 12 11 10  9  8  7 

Round 3. (1 plays 12, 13 plays 11, ... )

 1 13 14  2  3  4  5
12 11 10  9  8  7  6    

until you end up almost back at the initial position

Round 13. (1 plays 2, 3 plays 14, ... )

 1  3  4  5  6  7  8
 2 14 13 12 11 10  9

关于algorithm - 从列表中的元素创建所有可能的对,然后对它们进行均匀排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44944568/

相关文章:

algorithm - 用于提取特定行的 Excel 公式

r - 这种对 ROC 绘图的优化背后的基本原理是什么?

Haskell - 由于我不明白的原因,非穷举模式

Javascript 通过正则表达式 id 搜索有选择地清除表单输入

java - 多个项目之间的排序算法,而不仅仅是两个项目(篮球)之间的排序算法?

python - 通过相等的第一个字段组合/分组/合并列表列表,在字符串中加入第二个字段并在其他字段中写入相同的数据

python - Python 列表的复杂排序算法?

python - 如何按字母顺序完全排序 JSON 树

sorting - 最高 awk 结果被计数条件截断

java - 在 Java 中查找图像中的图像