l********s 发帖数: 358 | 1 I want generate the combination of several components. For example, I have 4
components which are labeled by 1, 2, 3, 4. So the unique different
combination will be 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134, 234,
1234. My idea is that I could create a set of combination, as: std::set< std
already exist in the set, since 123 is the same with 213, 231, and 321, and I just
need one combination.
Is there any better idea? Thanks a lot | k****f 发帖数: 3794 | 2 你的效率太低了
http://www.merriampark.com/comb.htm
4
,
std
and I just
【在 l********s 的大作中提到】 : I want generate the combination of several components. For example, I have 4 : components which are labeled by 1, 2, 3, 4. So the unique different : combination will be 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134, 234, : 1234. My idea is that I could create a set of combination, as: std::set< std : already exist in the set, since 123 is the same with 213, 231, and 321, and I just : need one combination. : Is there any better idea? Thanks a lot
| l********s 发帖数: 358 | 3 Thanks for the information, however I don't understand the algorithm in the
class. In the class "CombinationGenerator(int n, int r)", it requires r < n,
and in my case, I need r = n. | l********s 发帖数: 358 | 4 Hi, I used your keywords "combination generate" to google and find useful
information for my case.
http://www.programmersheaven.com/mb/beginnercpp/283845/283845/readmessage.aspx
Thanks for help again. | t****t 发帖数: 6806 | 5 你就不能多花点时间读读, 多花点时间想想么.
第一, 那个link里的程序是列出所有的C(n, r)组合, 不是\union_{r=1}^n C(n, r),
你需要自己循环r;
第二, 算法就是getNext()那一段, 除了这一段, 其它的部分都很蠢
第三, 你哪只眼睛看到说要求r
第四, 就算要求 r
the
n,
【在 l********s 的大作中提到】 : Thanks for the information, however I don't understand the algorithm in the : class. In the class "CombinationGenerator(int n, int r)", it requires r < n, : and in my case, I need r = n.
| l********s 发帖数: 358 | 6 Hey,
public CombinationGenerator (int n, int r) {
if (r > n) {
throw new IllegalArgumentException ();
}
I don't want to argue with you. But I really see r > n => throw exception.
Anyway, thanks for everyone's response and support. | t****t 发帖数: 6806 | 7 r>n的逆条件是啥?
r
多用脑,少用嘴
【在 l********s 的大作中提到】 : Hey, : public CombinationGenerator (int n, int r) { : if (r > n) { : throw new IllegalArgumentException (); : } : I don't want to argue with you. But I really see r > n => throw exception. : Anyway, thanks for everyone's response and support.
| c**l 发帖数: 12 | 8 这个本质上就是,keep index sorted.
8queen solution
For OP's question, create a n-element array of CombinationGenerator, let r
to be from 1 to n.
Good stuff.
你的效率太低了
http://www.merriampark.com/comb.htm
4
,
std
and I just
【在 k****f 的大作中提到】 : 你的效率太低了 : http://www.merriampark.com/comb.htm : : 4 : , : std : and I just
| s****u 发帖数: 118 | 9 int n = 4;
int elems[] = { 1, 2, 3, 4 };
for (int i = 0; i < (1 << n); ++i) {
for (int k = 0; k < n; ++k) if (i & (1 << k)) {
// outputs elems[k]...
}
}
4
,
std
it
and I just
【在 l********s 的大作中提到】 : I want generate the combination of several components. For example, I have 4 : components which are labeled by 1, 2, 3, 4. So the unique different : combination will be 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134, 234, : 1234. My idea is that I could create a set of combination, as: std::set< std : already exist in the set, since 123 is the same with 213, 231, and 321, and I just : need one combination. : Is there any better idea? Thanks a lot
|
|