如何生成的C / C表的所有组合的数组列表++组合、数组、列表

由网友(花间辞)分享简介:我想使不同的几个参数自动测量。有与含有可变的行/列数的参数值,例如,一个表:I want to make automated measurements varying several parameters.There is a table with variable row/column-number conta...

我想使不同的几个参数自动测量。 有与含有可变的行/列数的参数值,例如,一个表:

I want to make automated measurements varying several parameters. There is a table with variable row/column-number containing parameter values, e.g.:

a1 b1 c1
a2 b2 c2
a3 b3 c3

有一个简单的是,用于生成包含这样的列方向的所有组合阵列的清单:

Is there an easy was for generating list of Arrays containing all combinations in column direction like that:

a1 a2 a3
b1 a2 a3
c1 a2 a3
a1 b2 a3
b1 b2 a3
...
c1 c2 c3

3×3表应导致27组合(3!)。

3x3 Table should result in 27 combinations (3!).

该algorthm应当尽可能在C / C ++,STL / Qt的是也很大。

The algorthm should be if possible in C/C++, STL/Qt would be also great.

感谢您的任何提示!

P.S: 这看起来很容易,但由于2小时我曾坐在这个问题! : - (

P.S.: It looks easy, but I have sat on this problem since 2 hours already! :-(

推荐答案

您可以使用递归:

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
阅读全文

相关推荐

最新文章