如何生成的C / C表的所有组合的数组列表++ [英] How to generate array-list of all combinations of a table in C/C++

查看:144
本文介绍了如何生成的C / C表的所有组合的数组列表++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于如何生成的C / C表的所有组合的数组列表++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆