日期:2014-05-17  浏览次数:20750 次

求M选N算法
本帖最后由 caozhy 于 2013-08-16 11:53:21 编辑
int n=3;
 string[] are = { "1", "2","3", "4","5", "6" }; //数组不确定 可能更多 "7","8"......
生成n位不重复的数 
123 234 345 456  
124 235 346 
125 236 356 
126 245     
134 246 
135 256  
136     
145 
146  
156   

n=4
1234
1235
1236
1245
1246
1256
1345
.......
算法

------解决方案--------------------
排列组合吧
http://www.cnblogs.com/rogerwei/archive/2010/11/18/1880336.html
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 3;
            string[] are = { "1", "2", "3", "4", "5", "6" };
            var result = are.Select(x => new string[] { x });
            for (int i = 0; i < n - 1; i++)
            {
                result = result.SelectMany(x => are.Where(y => y.CompareTo(x.First()) < 0).Select(y => new string[] { y }.Concat(x).ToArray()));
            }
            foreach (var item in result)