【发布时间】:2018-09-02 07:18:08
【问题描述】:
我正在寻找解决问题的方法: 我必须编写一个代码来计算唯一元素的组合,即选择作为组的 n 个元素的所有不同组合 k 元素并重新计算剩余子集的新组合,无需复制。 给定 S,所有可能的唯一元素的集合,我必须计算 S 的元素的唯一组合的子集 T,现在我有 重新计算一个新的子集 - V - T 和所有子集 T 和 V 的组合必须是唯一的:
For example I have this set S: {0, 1, 2, 3, 4}
我必须得到
a {0, 1} {2, 3} { 4}
b {0, 1} {2, 4} { 3}
c {0, 1} {3, 4} { 2}
d {0, 2} {1, 3} { 4}
e {0, 2} {1, 4} { 3}
f {0, 2} {3, 4} { 1}
g {0, 3} {1, 2} { 4}
h {0, 3} {1, 4} { 2}
i {0, 3} {2, 4} { 1}
j {0, 4} {1, 2} { 3}
k {0, 4} {1, 3} { 2}
l {0, 4} {2, 3} { 1}
discarded as the same as g -> {1, 2} {0, 3} { 4}
discarded as the same as j -> {1, 2} {0, 4} { 3}
m {1, 2} {3, 4} {0}
discarded as the same as d -> {1, 3} {0, 2} { 4}
discarded as the same as k -> {1, 3} {0, 4} { 2}
n {1, 3} {2, 4}{ 0}
discarded as the same as e -> {1, 4} {0, 2} { 3}
discarded as the same as h -> {1, 4} {0, 3} { 2}
o {1, 4} {2, 3}{0}
discarded as the same as a -> {2, 3} {0, 1} { 4}
discarded as the same as l -> {2, 3} {0, 4} { 1}
discarded as the same as o -> {2, 3} {1, 4} { 0}
discarded as the same as b -> {2, 4} {0, 1} { 3}
discarded as the same as i -> {2, 4} {0, 3} { 1}
discarded as the same as n -> {2, 4} {1, 3} { 0}
discarded as the same as c -> {3, 4} {0, 1} { 2}
discarded as the same as f -> {3, 4} {0, 2} { 1}
discarded as the same as m -> {3, 4} {1, 2} { 0}
组合 {1, 2} {0, 3} { 4} 与 {0, 3} {1, 2} { 4} 的(对于这个问题)相同,然后必须丢弃,与 {1, 2} 相同{0, 4} { 3} 和 {0, 4} {1, 2} { 3}。
是否有可能在不使用考虑了已经获得的组合的数据结构(作为列表)的情况下达到目标?
我需要这样的东西:Generating Combinations: 1
这不是上一个问题的重复,因为研究涉及必须被认为是单义的分区,即其中包含的元素(无论它们的顺序如何)在以前的细分中必须不是已经同意的,例如 {1 , 2} {0, 4} { 3} 和 {0, 4} {1, 2} { 3} 必须被认为是非唯一的,因此只有组合有效:{0, 4} {1, 2} { 3}
【问题讨论】:
-
我可能弄错了,但在您的示例中,您为 5 个元素的独特组合提供了 24 个可能的结果。通常不是 120 个结果吗? {2, 1} {0, 3} {4} 在哪里?此外,T 子集的值是否强制配对?不能是 {0, 1, 2}{3, 4} 吗?你能给出一个 S 中 3 和 4 元素的完整例子吗?
-
@Tom 的 {2, 1} {0, 3} {4} 不存在,因为不同的订单(在一个集合内)被认为是相同的组合,所以 {2, 1} {0 , 3} {4} 与 {0, 3} {1, 2} {4} 相同。 T 不是强制配对的,在本例中是配对的。
-
因此,每个“组合”是一组无序的
n/k无序的k元素集,以及一个无序的n%k元素集,每个完整“组合”中的所有唯一元素,取自一组n独特元素,k固定。对我来说,这听起来很像一个分组问题(组合问题的一个子类型)。有趣.. -
@nullqube 感谢您的建议,但递归代码并没有完全按照要求执行,并且最后的非递归代码在分段错误中崩溃
标签: c algorithm combinations