我有一个用N = 9
创建的数组N%2
我想一次取出[[1,2],[3,4],[5,6],[7,8]]
个子阵列,没有数字出现两次,例如第一次出现:
[[1,3],[2,4],[5,7],[6,8]]
这意味着没有选择9。我第二次拿出
[[1,9],[2,9],[3,9],[4,9],[5,9],[6, 9],[7, 9],[8, 9]]
再次9没被挑选。 最终我会留下
students = [1,2,3,4,5,6,7,8,9]
available_combinations = a.combination(2).to_a
def make_pairs
available_students = students
while available_students.length > 1 do
student_combination = find_sub_array(available_combinations, available_students.first, available_students)
available_combinations.delete(student_combination)
available_students -= student_combination
end
end
def find_sub_array(available_combinations, student, available_students)
available_combinations.each do |subarray|
if subarray.include?(student) && (subarray - available_students).empty?
return subarray
end
end
end
这意味着我不能再选择4个没有重复数字的数组。
如何订购我的数组,以便每个数字只被遗漏一次而不是9?
用例如下:
{{1}}
我想多次运行make_pairs,直到available_combinations不再有足够的对。
答案 0 :(得分:0)
对于奇数,只需添加另一个虚拟数字,例如make clean
或0
:
:bye
现在,当你拉动n%2对时,至少有一对将与:再见。这是配对中的奇怪学生。每个学生将有相同数量的:再见轮次。
编辑:我假设你在这个例子中拉了5对,然后用:再见丢弃该对,以确保它不会重复。