开发者

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, I.E

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开发者_如何学Python 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? It's

0
1
2

And what does the N=2 case look like? 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. Now if you see the pattern for this specific case, can you say what the pattern is for the general case? i.e. 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? 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).

I would write one method (if not already there in the framework or a library) that would allow you to switch bases. I.E:

String transformNumberFrom10BaseToXBase(int number, int base)

then just write a for loop (sorry, pseudo-c-like-code :) ):

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)

    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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜