如何将组合硬编码片段转换为递归函数?

时间:2016-07-21 00:00:38

标签: c# algorithm recursion combinations hardcoded

目前我有这个代码片段,正在做我想要的事情:给定一些对象,它会创建它们之间的所有可能组合。

在此示例中,想象有4个对象(0,1,2和3),创建了4个对象的所有可能组合(0,1,2,3,0(0和1的组合) ,02,03,12,13,23,012,013,023,123和0123)。

应该注意的是,有2 ^ 4 - 1 = 15种组合,一般有2 ^个对象 - 1种组合。

使用此代码创建的对象的顺序为:0 - > 01 - > 012 - > 0123 - > 013 - > 02 - > 023 - > 03 - > 1 - > 12 - > 123 - > 13 - > 2 - > 23 - > 3

我获取初始对象的方式及其数量在代码中的其他位置定义。

int count = 4; //this is gotten elsewhere
int currPos = 0;
var objects = new Object[(2^count)-1];

for (int i = 0; i < count; i++) //loop that creates combinations of only one object
{
    Object obj = new Object(...);
    objects[currPos] = obj;
    currPos += 1;

    for (int j = i + 1; j < count; j++) //loop that creates combinations of two objects
    {
        Object obj = new Object(...);
        objects[currPos] = obj;
        currPos += 1;

        for (int k = j + 1; k < count; k++) //loop that creates combinations of three objects
        {
            Object obj = new Object(...);
            objects[currPos] = obj;
            currPos += 1;

            for (int l = k + 1; l < count; l++) //loop that creates combinations of four objects
            {
                Object obj = new Object(...);
                objects[currPos] = obj;
                currPos += 1;
            }
        }
    }
}

尽管给出了正确的结果,但这是硬编码的,因此我正在寻找一种方法将其更改为递归函数(但维护其功能),具有对象的数量(这也决定了最大组合,四个例子)作为参数传递。

我一直在尝试做类似下面的代码,但没有结果,主要是因为我似乎无法在必要时转到“上一个”循环,例如从0123到013。

    int count = 4;
    int currPos = 0;
    var objects = new Object[(2^count)-1];
    combinations(count, 0, currPos, objects); //called elsewhere

    private void combinations(int numberOfObjects, int j, int count, int currPos, Object[] objects)

    {   
        if (numberOfObjects == count)
        {
            for (int k = j; k < count; k++)
            {
                Object obj = new Object(...);
                objects[currPos] = obj;
                currPos += 1;
                generateCombinations(numberOfObjects - 1, j + 1, count, currPos, objects);
            }
        }

        if (numberOfObjects < count)
        {
            for (int l = j; l < count; l++)
            {
                Object obj = new Object(...);
                objects[currPos] = obj;
                currPos += 1;

                (...)

                generateCombinations(..., ..., count, currPos, objects);
             }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是你追求的那种吗?

public IEnumerable<string> GetCombinations(IEnumerable<string> source)
{
    if (source == null || !source.Any())
    {
        return Enumerable.Empty<string>();
    }
    else if (source.Skip(1).Any())
    {
        return new string[] { null, source.First() }.SelectMany(x => GetCombinations(source.Skip(1)), (x, y) => x + y);
    }
    else
    {
        return new string[] { null, source.First() };
    }
}

我可以这样使用它:

var combinations = GetCombinations(new[] { "0", "1", "2", });

我得到了这个结果:

null  
2 
1 
12 
0 
02 
01 
012