我正在努力制作netlogo中的程序,它将海龟的不同组合分成两组。 例如;系统中共有10只海龟存在[0 1 2 3 4 5 6 7 8 9],我想将这些海龟的不同组合分成两组。 {[0 6 7 8] [1 2 3 4 5 9]},{[2 3 6 8 9] [0 1 4 5 7]} .......等等。
任何帮助都会非常感激。
答案 0 :(得分:0)
这可能就是你想要的。下面的代码通过排序列表并删除重复项来删除重复项(即它会删除[[1 2] [3]]和[[2 1] [3]],因为它会排序第一个列表,然后是[1 2]这将导致比赛)。切掉一个元素并将其插入左子列表并递归。
您可以想象列表中的每个数字都与谁对应。所以你可以做到
let alist [who] of turtles
如果您有任何问题,请告诉我
to-report permutate-sublist [added-whos remaining-whos]
ifelse length remaining-whos = 1
[
report (list (list (sort added-whos) (sort remaining-whos)))
]
[
let result (list)
foreach (sort remaining-whos)
[
let x ?
let new-whos (sentence added-whos x)
let new-remaining-whos (remove x remaining-whos)
set result (sentence (list (list (sort new-whos) (sort new-remaining-whos))) result)
set result (remove-duplicates (sentence (permutate-sublist new-whos new-remaining-whos) result))
]
report result
]
end
打印出
的结果时会发生这种情况let alist (list 1 2 3 )
show permutate-sublist (list) alist
[[[2 3] [1]]
[[1 3] [2]]
[[3] [1 2]]
[[1 2] [3]]
[[2] [1 3]]
[[1] [2 3]]]