2014-07-16 113 views
-2

我有数字的字符数组:递归得到所有可能的排列,从一个数组

static char[] numbers = {'0','1','2','3','4','5','6','7','8','9'}; 

,我需要做的是将它们组合中的所有更多钞票组合多达3个位数号码的功能。像这样:

0 
1 
2 
3 
00 
01 
02 
03 
10 
11 
12 
13 
20 
21 
22 
23 
30 
31 
32 
33 
000 
001 
002 
003 
010 
011 

像这样,但所有posible组合。 到目前为止,我已经使用循环与如果里面,但我发现,会更好地使用递归函数为 什么是最好和最快的方式来做到这一点?

+2

简短易读或快速?您可以使用这个快速实施,它也值得一读:http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-CG –

+0

最快的计算机时间为任务,但也编码它的最佳方式。 – Halosecrets0032

+0

除非你想要并行编程,否则最好的办法就是按照自然的方式来做 - 从字符0到9开始的函数从所有字符开始从0到9开始最多组合3个组合。但是你必须真正做出基准测试才能找到真正的答案。 –

回答

0

嗯,这是我想出的代码。

static char[] numbers = {'0','1','2','3','4','5','6','7','8','9'}; 
    static bool continuar = true; 

    private static void Start(int maxLength) 
    { 
     for (int i = 0; i <= maxLength; i++) 
      Permutations(i, 0, ""); 
    } 

    private static void Permutations(int keyLength, int position, string baseString) 
    { 
     bool print = true; 
     if (continuar) 
     { 
      for (int i = 0; i < numbers .Length; i++) 
      { 
       string temp = baseString + numbers [i]; 
       if (position <= keyLength - 1) 
       { 
        Permutations(keyLength, position + 1, temp); 
        print = false; 
       } 
       if (continuar && print) 
        Console.WriteLine(temp); 
      } 
     } 
    } 
相关问题