自己写的一个全排列算法,请各位指正。

 

Code
namespace permutation
{
    
class Program
    {
        
        
static void Main(string[] args)
        {
            
for (int i = 0; i < MAX; i++)
            {
                chars[i] 
= (char)((int)('a'+ i);
                mark[i] 
= 0;
            }

            Permutate();
            Console.WriteLine(
"Total count is: {0}", count);
        }

        
private static int MAX = 8;
        
private static int count = 0;
        
private static char[] chars = new char[MAX];
        
private static char[] output = new char[MAX];
        
private static int[] mark = new int[MAX];
        
private static int output_index = 0;


        
private static void Permutate()
        {
            
for (int i = 0; i < MAX; i++)
            {
                
if (mark[i] == 0)
                {
                    output[output_index] 
= chars[i];
                    output_index
++;

                    
if (output_index == MAX)
                    {
                        count
++;
                        
for (int ii = 0; ii < MAX; ii++)
                            Console.Write(output[ii]);
                        Console.WriteLine();
                    }

                    mark[i] 
= 1;
                    Permutate();
                    mark[i] 
= 0;
                    output_index
--;
                }
            }
        }
    }
}

 

运行已经没有错误,只是想知道怎么能优化一下。

posted on 2009-05-13 23:46  smwikipedia  阅读(247)  评论(0编辑  收藏  举报

导航