如何创建所有数字组合的n维数组? [英] How to create a n-dimensional array of all combination of number?

查看:63
本文介绍了如何创建所有数字组合的n维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个函数 AllCombnations(d,maxValue),该函数将创建一个从0到 maxValue的所有数字组合的d维数组

I wish to create a function AllCombnations(d, maxValue) which will create a d-dimensions array of all number combinations from 0 to maxValue.

例如,在3D空间中创建所有数字组合的硬编码版本,范围从0到 maxValue 可能类似于:

For example, a hardcoded version of creating all number combinations in 3D space, from 0 to maxValue would possibly be something like:

for (int i = 0; i < maxValue; i++)
    for (int j = 0; j < maxValue; j++)
        for (int k = 0; k < maxValue; k++)
        {
            // code here
        }

我面临的问题是我无法嵌套 n 用于循环,并且不确定如何处理。我考虑过递归,但没有成功。任何帮助将不胜感激。

The issue I face is that I cannot nest n for loops, and am unsure how I would go about this. I have considered recursion, but have had no success. Any help would be greatly appreciated.

推荐答案

我现在知道这是一篇旧文章,但是我为这个问题创建了解决方案。

I know this is an old post now, but I DID create a solution to this problem.

让我用示例脚本解决这个问题。

Let me go through this issue with an example script.

class Program
{
    static void Main()
    {
        // Print all combinations from a to b, for n dimensions
        // e.g. 0000 to 2222 <- each dimension goes from 0 to 2, with 4 dimensions
        // Note that each dimension can have a unique start/end point
        // e.g. 1234 to 5678, so the 2nd dimensions is bound 2 <= x <= 6

        int dimensions = 4;
        int[] startValues = { 0, 0, 0, 0 };
        int[] endValues = { 2, 2, 2, 2 };

        PrintCombinations(startValues, endValues, dimensions);

        Console.ReadKey();
    }

    /// <summary>
    /// Prints all combinations of numbers given inputs
    /// </summary>
    /// <param name="start">Inclusive stating integers</param>
    /// <param name="end">Inclusive ending integers</param>
    /// <param name="dimensions">The number of dimensions to iterate</param>
    private static void PrintCombinations(int[] startValues, int[] endValues, int dimensions)
    {
        // Create new array to loop through without disturbing the original array
        int[] loopArray = (int[])startValues.Clone();

        // Loop through each value
        while (!Enumerable.SequenceEqual(loopArray, endValues))
        {
            // Write array to console
            Console.WriteLine($"{string.Join(", ", loopArray)}");

            // Increment array
            loopArray[0]++;

            // Check if a dimension is larger than it's maximum, then set to min, and add +1 to next dimension
            // Do not do this for last dimension, as loop will break once the final combination is met
            for (int i = 0; i < dimensions - 1; i++)
                if (loopArray[i] > endValues[i])
                {
                    loopArray[i] = startValues[i];
                    loopArray[i + 1]++;
                }
        }

        // Write final array combination  to console
        Console.WriteLine($"{string.Join(", ", loopArray)}");
    }
}

这是一个非常简单的示例,足以说明我想扩展以数组表示的多维的概念。

This is a simple enough example to show how exactly I wanted to expand on the idea of "multiple dimensions" represented as an array.

如果您查看 PrintCombinations ,您将看到以下代码:

If you look to the bottom of PrintCombinations, you will see the following code:

for (int i = 0; i < dimensions - 1; i++)
    if (loopArray[i] > endValues[i])
    {
        loopArray[i] = startValues[i];
        loopArray[i + 1]++;
    }

这是我通过多个维度提出的代码,删除了当用户提交了尺寸和其他信息时,需要对循环进行硬编码(如上例所示)。

This is the code I come up with the loop through multiple dimensions, removing the need to hard-code loops when you have user submitted dimensions and other information (as shown in the upper example).

基本上,此代码将每个尺寸的值存储在数组。
让我们以3个维度(x,y,z)为例。
我们可以说点(x,y,z)= int [] {x,y,z}
如果我们说x,y和z是数组的上限,我们可以通过减去数组的第一个维度来遍历此数组,直到达到零,然后从后续维度中删除一个,直到达到零,依此类推,同时在执行此操作时将维度重置为上限,或者如本例所示,添加从零到上限,然后重置为零,并增加以下尺寸。

Basically, this code stores the VALUE of each dimension in an array. Let us do an example of 3 dimensions, (x, y, z). We can say the point (x, y, z) = int[] { x, y, z } If we say x, y, and z are the upper bound of the array, we can loop through this array by subtracting the array's first dimesnsion, until it reaches zero, then remove one from the following dimension until it reaches zero, etc, all while resetting the dimension to the upper bound when doing so, or as in this example, add from zero to an upper bound, then reset to zero, and increment the following dimension.

通过使用更多数组作为上下限,您可以在两个之间进行嵌套循环两个特定范围。在上面的示例中,我使用了 {2,2,2,2} 的上限。

By using further arrays for upper and lower bounds, you can essentially make nested loops between two specific ranges. In the above example, I used an upper bound of { 2, 2, 2, 2 }.

I希望我已经解释清楚了。谢谢

I hope I have explained this well. Thanks

这篇关于如何创建所有数字组合的n维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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