我正在尝试构建一个配对生成器。它需要六个名称的列表,并生成一周(5 天)的对,并尽可能少地重复。
我的复制对最少为 2 个(因此我找到了 5 天的对,即总共 15 个对组合,其中只有 2 个相同的组)。
我的方法:
# Start with individuals in an array
[1, 2, 3, 4, 5, 6]
# Bisect the array
[1, 2, 3]
[4, 5, 6] => yields pair combinations [1, 4], [2, 5], [3, 6]
# Move the lower of the bisected arrays along
[1, 2, 3]
[6, 4, 5] => yields pair combinations [1, 6], [2, 4], [3, 5]
# Move along once more
[1, 2, 3]
[5, 6, 4] => yields pair combinations [1, 5], [2, 6], [3, 4]
# Since there are no more unique pair combinations, bisect each array again
(Array 1) [1, 2]
(Array 1) [3] => yields pair combination [1, 3] with 2 'spare'
(Array 2) [4, 5]
(Array 2) [6] => yields pair combination [4, 6] with 6 'spare'
=> 'spare' pair combination [2, 6] is a replication
# Move the lower of the bisected arrays along
(Array 1) [1, 2]
(Array 1) [3] => yields pair combination [2, 3] with 1 'spare'
(Array 2) [4, 5]
(Array 2) [6] => yields pair combination [5, 6] with 4 'spare'
=> 'spare' pair combination [1, 4] is a replication
上面的过程为我们提供了 13 个唯一对,然后是 2 个非唯一对。一周中的每一天都会被覆盖,但我们会复制。
有什么方法可以更有效地做到这一点/避免复制吗?
最佳答案
这是一场循环赛,每个玩家都与其他玩家进行比赛。将玩家如下所示排列成 1 4、2 5 和 3 6 组:
123
456
固定球员1,轮换其余球员:
142
563
生成对 1 5、4 6 和 2 3. 继续旋转:
154
第632章
165
324
136
245
关于ruby - 尝试创建一个对生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464154/