举个例子:
三个数:1、2
组成3位数,有以下这些:
111
112
121
122
211
212
221
222现求一个算法,由N个数,组成M位数,的所有字符串集合

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] metachars = { "1", "2" };
                int n = 3;
                var result = metachars.AsEnumerable();
                for (int i = 0; i < n - 1; i++)
                {
                    result = result.SelectMany(x => metachars.Select(y => x + y));
                }
                Console.WriteLine(string.Join("\r\n", result.ToArray()));
            }
        }
    }111
    112
    121
    122
    211
    212
    221
    222
    Press any key to continue . . .