有序的独特结合, [英] Ordered Unique Combinations

查看:100
本文介绍了有序的独特结合,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

internal class Course
{
    public int CourseCode { get; set; }
    public string DeptCode { get; set; }
    public string Name { get; set; }
}

和下面的代码是2维数组我有:

and the following code is the 2 dimensional array I have:

Course[][] courses = new Course[3][];
courses[0] = new Course[] {
   new Course() { CourseCode = 100, DeptCode = "EGR", Name = "EGR A" },
   new Course() { CourseCode = 100, DeptCode = "EGR", Name = "EGR B" }
};

courses[1] = new Course[] {
   new Course() { CourseCode = 200, DeptCode = "EN", Name = "EN A" }
};

courses[2] = new Course[] {
   new Course() { CourseCode = 300, DeptCode = "PHY", Name = "PHY A" }
};



我想要做的就是让不同的组合,一个组中的每个项目可以与其他做些什么组;例如与前面的代码,结果将是:

What I want to do is get the different combinations that each item in a group could do with the other groups; for example with the previous code, the results would be:

1. EGR A - EN A - PHY A
2. EGR B - EN A - PHY A

回答:
要获取组合可能的数量,我们可以使用产品的规则在上述情况下,可能的组合为(2 * 1 * 1)= 2,它确实是2组合我上面写

Answers: To get the number of combinations possible, we can use Rule of Product, in the above case, the possible combinations will be (2 * 1 * 1) = 2 which is indeed the 2 combinations I wrote above.

LordTakkera给了完美!回答,太谢谢你了。

LordTakkera has given the perfect answer, thank you so much!

推荐答案

您可以使用一个嵌套的循环:

You could use a nested for loop:

for (int i = 0; i < courses[0].Length; i++)
{
   for (int j = 0; j < courses[1].Length; i++)
   {
     for (int k = 0; k < courses[2].Length; i++)
     {
         //Do whatever you need with the combo, accessed like:
         //courses[0][i], courses[1][j], courses[2][k]
     }
   }
}

当然,这个解决方案变得非常凌乱你需要更多的嵌套。如果你需要去任何更深,我会用某种形式的递归函数来遍历集合,并产生组合

Of course, this solution gets really messy the more nesting you need. If you need to go any deeper, I would use some kind of recursive function to traverse the collection and generate the combinations.

这将是这样的:

class CombinationHelper
{
    public List<List<Course>> GetAllCombinations(Course[][] courses)
    {
            return GetCourseCombination(courses, 0);
    }

    public List<List<Course>> GetCourseCombination(Course[][] courses, int myIndex)
    {
        List<List<Course>> combos = new List<List<Course>>();

        for (int i = 0; i < courses[myIndex].Length; i++)
        {
            if (myIndex + 1 < courses.GetLength(0))
            {
               foreach (List<Course> combo in GetCourseCombination(courses, myIndex + 1))
               {
                  combo.Add(courses[myIndex][i]);
                  combos.Add(combo);
               }
            }
            else
            {
               List<Course> newCombination = new List<Course>() { courses[myIndex][i] };
               combos.Add(newCombination);
            }
        }
        return combos;
    }
}



我测试了这个(代以诚信为课程使验证更容易),它生产的所有8种组合(虽然不是为了,递归趋于做到这一点。如果我拿出订货代码,我会后,但它不应该的困难)。

递归函数是很难足以让我拿出,所以我的解释不会很好。基本上,我们通过用0索引踢整个事情关闭(使我们从头开始)开始。然后,我们遍历当前数组。如果我们不主数组中最后一个阵列,我们递归到下一子阵列。否则,我们创建了一个新的组合,自己添加到它,然后返回。

Recursive functions are hard enough for me to come up with, so my explanation won't be very good. Basically, we start by kicking the whole thing off with the "0" index (so that we start from the beginning). Then we iterate over the current array. If we aren't the last array in the "master" array, we recurse into the next sub-array. Otherwise, we create a new combination, add ourselves to it, and return.

由于递归栈开卷,我们生成的组合添加到退货列表中添加自己它,并再次返回。最终,整个事情开卷和你留下的所有组合的一个列表。

As the recursive stack "unwinds" we add the generated combinations to our return list, add ourselves to it, and return again. Eventually the whole thing "unwinds" and you are left with one list of all the combinations.

同样,我敢肯定,这是一个非常混乱的解释,但递归算法(至少对我来说)固有的混乱。我会很乐意尝试阐述你想要的任何一点。

Again, I'm sure that was a very confusing explanation, but recursive algorithms are (at least for me) inherently confusing. I would be happy to attempt to elaborate on any point you would like.

这篇关于有序的独特结合,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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