请教各位高手:现在有几个词语:厂商、版本、平台、分辨率、创建时间、适配时间我想把这几个词语自由组合把这些自由组合的结果全部输出出来,
组合是任意的,可以2个词语,也可以1个词,更多也可以,只要不大于词语的总数就行

解决方案 »

  1.   

    首先你要输出在哪里你想自由组合是随机的么。
    建议你是有 枚举enum
    或者自定义结构体
      

  2.   

    list<string> l = new list<string>();
    l.add("厂商");  
    l.add("版本"); 
    l.add("平台"); 
    l.add("分辨率"); 
    l.add("创建时间"); 
    l.add("适配时间"); 然后通过随机数字 来去list中的字符串,需要注意的是 list是从0开始的
    list(0)是“厂商”生成不重复随机数字的方法
    public static string RandomNumber(int length)
    {
      string result = "";
      Random random = new Random(unchecked((int)DateTime.Now.Ticks));
      for (int i = 0; i < length; i++)
      {
        result += random.Next(10).ToString();
      }
      return result;
    }
      

  3.   


    static void Main(string[] args)
            {
                Random ran = new Random();
                for (int i = 0; i < 10; i++)
                {                
                    Console.WriteLine(GetRandomFromSource(ran));
                }
            }        public static string GetRandomFromSource(Random ran)
            {
                string result = "";
                string[] source = { "厂商", "版本", "平台", "分辨率", "创建时间", "适配时间" };            
                var tempRet = new List<string>();            
                int current = ran.Next(1 << source.Length);
                for (int j = 0; j < source.Length; j++)
                {
                    if (((1 << j) & current) != 0)
                    {
                        tempRet.Add(source[j]);
                    }
                }            foreach (var t in tempRet)
                {
                    result += t;               
                }
                return result;            
            }
      

  4.   


    class Program
        {
            List<string> result = new List<string>();
            string[] source = { "厂商", "版本", "平台", "分辨率", "创建时间", "适配时间" };
            int totalLoop;
            int currentLoop;
            static void Main(string[] args)
            {
                Program p = new Program();
                List<string> result = p.GetRandomFromSource();
                int number = 0;
                for (int i = 0; i < result.Count; i++)
                {
                    Console.WriteLine(result[i]);
                    number++;
                }
                Console.WriteLine("Total is " + number + " lines");
                Console.ReadLine();
            }        private List<string> GetRandomFromSource()
            {
                totalLoop = source.Length;
                currentLoop = 1;
                Loop();
                return result;
            }        private void Loop()
            {
                if (currentLoop == 1)
                {
                    for (int k = 0; k < source.Length; k++)
                    {
                        result.Add(source[k]);
                    }
                    if (currentLoop == totalLoop)
                        return;
                }
                currentLoop++;
                int count = result.Count;
                for (int k1 = source.Length * (currentLoop - 2); k1 < count; k1++)
                {
                    for (int k2 = 0; k2 < source.Length; k2++)
                    {
                        string temp = result[k1] + "," + source[k2];
                        result.Add(temp);
                    }
                }
                if (currentLoop == totalLoop)
                    return;
                Loop();
            }
        }
    方法有点笨,不过应该可行吧,试试~