简体   繁体   English

C#中的特定组合,无法掌握

[英]Specific Combinations in C#, unable to grasp it

I've been looking into combinations lately, I've tried various 3rd party solutions, and tried to get my head around it myself. 最近,我一直在研究组合,我尝试了各种第三方解决方案,并试图自己解决这个问题。 Without success I might add. 没有成功,我可能会补充。

I need to generate a 13 length string with all possible combinations of say.. int 0-2, IE 我需要生成一个长度为13的字符串,并说所有可能的组合。int 0-2,IE

0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0 0 0 1 0
... 
2 2 2 2 2 2 2 2 2 2 2 2 2

You probably get the drill, I'm aware I could just wrap this up in loops if I wanted a dirty solution. 您可能会开始练习,我知道如果我想要一个肮脏的解决方案,我可以将其循环包装。 Any guidance or pointers are appreciated. 任何指导或指针,不胜感激。

I'd be happy to write the code for you, but it seems like you are looking for intuition into the problem. 我很乐意为您编写代码,但似乎您正在寻找对问题的直觉。 So maybe this feels more like a "teach a man to fish" moment. 因此,也许这更像是“教人钓鱼”的时刻。

So let me ask you a few questions: 所以让我问你几个问题:

Let's generalize the problem to strings of length N. What does the N=1 case look like? 让我们将问题推广到长度为N的字符串。N = 1的情况是什么样的? It's 它的

0
1
2

And what does the N=2 case look like? N = 2的情况是什么样的? It's 它的

00
01
02
10
11
12
20
21
22

I wonder if you can see how, given the N=1 case, we can easily derive (aka generate) the N=2 case in a very mechanical fashion. 我想知道您是否可以看到,在N = 1的情况下,我们如何以非常机械的方式轻松地推导(又称为生成)N = 2的情况。 Now if you see the pattern for this specific case, can you say what the pattern is for the general case? 现在,如果您看到此特定情况的模式,您能否说出一般情况的模式? ie If you happened to already have in your hand the answer for strings of length N, and I asked you to provide me with the answer for strings of length N+1 could you do so? 即,如果您碰巧已经掌握了长度为N的字符串的答案,而我想请您提供长度为N + 1的字符串的答案,可以吗? If so you have the very definition of a recursive algorithm. 如果是这样,则您具有递归算法的定义。

PS I think I'd call these combinations rather than permutations. 附言:我认为我称这些组合而不是排列。

I can't help thinking of this as just adding number in a N-based numeric system (in your example a 3-base system). 我不禁将其视为仅在基于N的数字系统(在您的示例中为3基系统)中加数字。

I would write one method (if not already there in the framework or a library) that would allow you to switch bases. 我会写一个方法(如果框架或库中还没有),则可以切换基础。 IE: IE:

String transformNumberFrom10BaseToXBase(int number, int base)

then just write a for loop (sorry, pseudo-c-like-code :) ): 然后只需编写一个for循环(对不起,类似伪c的代码:)):

for (int i = 0; i < base ^ 13; i++) {
    print transformNumberFrom10BaseToXBase(i, base)
}

Ok, hope that helps or give you an idea on how to solve it :) 好的,希望对您有所帮助或对您有所帮助:)

I've written quickly function that returns list of permutations, so if you have not time to write your own method, you can use it. 我已经快速编写了返回排列列表的函数,因此,如果您没有时间编写自己的方法,则可以使用它。 length is permutation length, max is maximum number (in your case, it would be 2) length是排列长度,max是最大数目(在您的情况下为2)

    static List<int[]> getPermutationList(int length, int max)
    {
        List<int[]> perm_list = new List<int[]>();
        int[] perm = new int[length];
        for (int i = 0; i < length; ++i)
            perm[i] = 0;
        while(true)
        {
            for (int i = length - 1; ; --i)
            {
                if (i == -1)
                    return perm_list;
                if (perm[i] < max)
                {
                    perm[i]++;
                    for (int j = i + 1; j < length; ++j)
                        perm[j] = 0;
                    break;
                }

            }
            int[] perm_copy = new int[length];


            for (int i = 0; i < length; ++i)
            {
                perm_copy[i] = perm[i];
            }
            perm_list.Add(perm_copy);
        }
        return perm_list;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM