数字排列组合

//不重复排列组合N!

public class Program
    {
        static List<string> sorts = new List<string>();
        static void Main(string[] args)
        {
            GetSortNums(4, "");
            for(int i = 0; i < sorts.Count; i++)
            {
                Console.Write(sorts[i] + "\t");
                if ((i+1) % 4==0)
                {
                    Console.WriteLine();
                }
            }
            
            Console.ReadKey();
        }
        public static void GetSortNums(int length, string combins)
        {
            for (int a = 0; a < length; a++)
            {
                string newcombins = combins;
                if (!newcombins.Contains(a.ToString()))
                {
                    newcombins += a;
                    if (newcombins.Length == length)
                    {
                        if (!sorts.Contains(newcombins))
                        {
                            sorts.Add(newcombins);
                        }

                    }
                    else
                    {
                        GetSortNums(length, newcombins);
                    }

                }

            }

        }
    }

//重复排列组合N^P

public static void GetSortSigns(int length, int depth, string combins)
        {
            for (int a = 0; a < length; a++)
            {
                string newcombins = combins;
                newcombins += a;
                if (newcombins.Length == depth)
                {
                    if (!sorts.Contains(newcombins))
                    {
                        sorts.Add(newcombins);
                    }

                }
                else
                {
                    GetSortSigns(length, depth, newcombins);
                }

            }

        }

posted @ 2016-10-21 16:57  木美琉璃  阅读(386)  评论(0)    收藏  举报