【问题标题】:All possible combinations of a 2D array [duplicate]二维数组的所有可能组合[重复]
【发布时间】:2012-07-04 18:40:02
【问题描述】:

我想从 2D [m x n] 数组中生成所有可能的组合,每个数组的第一个元素除外。该元素将代表表示其余元素的“类型”。例如,如果我有一个数组

shirts[][] = 
{
  {"colour", "red", "blue", "green", "yellow"},
  {"cloth", "cotton", "poly", "silk"},
  {"type", "full", "half"}
};

所需的输出应该是衬衫所有可能性的组合。对于上面的例子,

colour red
colour blue
...
cloth silk
type full
type half
colour red cloth cotton
colour red cloth poly
...
colour yellow type half
cloth cotton type full
...
cloth silk type half
colour red cloth cotton type full
...
colour yellow cloth silk type half

我尝试过这样的事情(也从其他 Stack Overflow Question 获得帮助)

String shirts[][] = 
{
  {"colour", "red", "blue", "green", "yellow"},
  {"cloth", "cotton", "poly", "silk"},
  {"type", "full", "half"}
};

majorCombinations = new int[possibilities][shirts.length];

int currentCombination;
int offset = 1;

for (int i=0; i < shirts.length; i++)
{
    currentCombination = 0;
    while (currentCombination < possibilities)
    {
        for (int j=0; j < shirts[i].length; j++)
        {
            for (int k=0; k < offset; k++)
            {
                if (currentCombination < possibilities)
                {
                    majorCombinations[currentCombination][i] = shirts[i][j];
                    currentCombination++;
                }
            }
        }
    }
    offset *= shirts[i].length;
}

但它只给出所有 n 个组合的值,即

colour cloth type
colour cloth full
...
yellow silk half

它没有考虑较小的组合,它甚至不是通用的,即对于 [m x n] 数组(n 不需要固定)。非常感谢 VBA 的帮助。我对 C、Java 和 C# 很熟悉。 在此先感谢:)

编辑:

这与question asked here 不同。这不是笛卡尔积,其中 one 元素取自每个相关数组。我需要的输出没有这个限制;因此,这种情况下的组合数 > 链接问题中的组合数。 此外,第一列是内容的描述,必须伴随内容。

【问题讨论】:

  • 也欢迎提供指向类似已回答问题的链接。
  • j1 开始而不是0 不是有帮助吗?
  • @Liam 编辑了为什么这不是重复的

标签: c# java vba combinations multidimensional-array


【解决方案1】:

Link to Original Answer

对于两个数组,应该使用两个嵌套循环:

for (int i = 0 ; i != c[0].length ; i++) {
    for (int j = 0 ; j != c[1].length ; j++) {
        System.out.writeln(""+c[0][i]+c[1][j]);
    }
}

对于更多嵌套,您需要递归或等效的基于堆栈的解决方案。

void combos(int pos, char[][] c, String soFar) {
    if (pos == c.length) {
         System.out.writeln(soFar);
         return;
    }
    for (int i = 0 ; i != c[pos].length ; i++) {
        combos(pos+1, c, soFar + c[pos][i]);
    }
}

【讨论】:

  • 可以正常工作到一个极限,即如果我有一个长度为 m 的二维数组,它会产生大小为 m 的组合。我需要的是尺寸 1 到 m 的组合。感谢您花时间回答;它应该提供一些见解。
【解决方案2】:

你想要的是笛卡尔积吗?

var colours = new[]{"colour - red", "colour - blue", "colour - green", "colour - yellow"};
var cloth = new[] {"cloth - cotton", "cloth - poly", "cloth - silk"};
var type = new[]{"type - full", "type - half"};

var combinations = from c in colours
                   from cl in cloth
                   from t in type
                   select new[]{c, cl, t};

【讨论】:

  • 谢谢戴夫;这种方法非常好。它在 SQL 中工作,但我需要更多类似 Java 的方法。
【解决方案3】:
for (int i = 0; i < shirts[0].length; i++) {
        for (int j = 0; j < shirts[1].length; j++) {
            for (int k = 0; k < shirts[2].length; k++) {
if (i != 0)
                    System.out.print(shirts[0][0] + " " + shirts[0][i]
                            + " ");
                if (j != 0)
                    System.out.print(shirts[1][0] + " " + shirts[1][j]
                            + " ");
                if (k != 0)
                    System.out.print(shirts[2][0] + " " + shirts[2][k]
                            + " ");
                System.out.println();
            }
        }

    }
}

【讨论】:

  • 这很简单,很棒!它适用于 3 个数组。
猜你喜欢
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2013-03-03
  • 2017-03-27
  • 2023-03-20
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多