求一段程序的算法,0,1,3 三个数字组成一个四位或者更多位的数字串. 求0,1,3三个组字组成的4位数字的排列
示例数据如:
100   0   300   1000   1100   1300   3000   3100   3300   
101   1   301   1001   1101   1301   3001   3101   3301   
103   3   303   1003   1103   1303   3003   3103   3303   
110   10   310   1010   1110   1310   3010   3110   3310   
111   11   311   1011   1111   1311   3011   3111   3311   
113   13   313   1013   1113   1313   3013   3113   3313   
130   30   330   1030   1130   1330   3030   3130   3330   
131   31   331   1031   1131   1331   3031   3131   3331   
133   33   333   1033   1133   1333   3033   3133   3333
就是说生成的最大字符串的长度可能大于4.现在我这个示例最长是4位的.或者暂时以最长4位来算.有算法就可以调整了.

解决方案 »

  1.   

    参考http://www.cnblogs.com/rogerwei/archive/2010/11/18/1880336.html
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication10
    {
        class Program
        {
            public static String[] A ={ "0","1" ,"3"};
            static void Main(string[] args)
            {
                min_miax(1, 4);
                Console.ReadKey();
            }
            public static void min_miax(int min, int max)
            {
                if (min == 0)
                {
                    find(max, "");
                }
                else
                {
                    for (int i = min; i <= max; i++)
                    { find(i, ""); }
                }        }        public static void find(int flag, String str)
            {
                if (flag == str.Length)
                {
                    if (str.Substring(0, 1) != "0")
                    {
                        Console.WriteLine(str);
                    }
                    else if(str.Length==1)
                    {
                        Console.WriteLine("0");
                    }
                    //return str;
                }
                else
                {
                    for (int i = 0; i <= A.Length - 1; i++)
                    {
                        //if (!str.Contains(A[i]))
                        //{
                        find(flag, str + A[i]);
                        //}
                    }
                }
                //return "";
            }
        }
    }