我想根据多个参数进行自动测量。 有一个包含参数值的可变行/列号的表,例如:
a1 b1 c1
a2 b2 c2
a3 b3 c3
是否有一个简单的方法可以生成包含列方向上所有组合的数组列表,如下所示:
a1 a2 a3
b1 a2 a3
c1 a2 a3
a1 b2 a3
b1 b2 a3
...
c1 c2 c3
3x3 表应产生 27 种组合 (3!)。
如果可能的话,算法应该是 C/C++,STL/Qt 也很好。
感谢您的任何提示!
附注: 看起来很简单,但我已经在这个问题上坐了两个小时了! :-(
最佳答案
您可以使用递归:
int selection[rows]; // Stores which item is selected for each row
void func(int row_num) {
if (row_num == rows) { // If we've selected for all the rows
// Do your thing with selection[]
return;
}
for (int i = 0; i < columns; i++) { // For each possible selection you can make row_num
selection[row_num] = i; // Choose it
func(row_num + 1); // Recurse over all possible combinations for the remaining rows
}
}
func(0); // Goes over all possibilities
关于c++ - 如何在 C/C++ 中生成表的所有组合的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7507682/