求M选N算法

weeetcom 2013-08-16 10:57:35
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
.......
...全文
1218 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
oShirt1 2014-10-20
  • 打赏
  • 举报
回复
= =那代码真是难看懂也...完全没接触过
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
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)
            {
                Console.WriteLine(string.Join(", ", item));
            }
        }
    }
}
1, 2, 3 1, 2, 4 1, 3, 4 2, 3, 4 1, 2, 5 1, 3, 5 2, 3, 5 1, 4, 5 2, 4, 5 3, 4, 5 1, 2, 6 1, 3, 6 2, 3, 6 1, 4, 6 2, 4, 6 3, 4, 6 1, 5, 6 2, 5, 6 3, 5, 6 4, 5, 6 Press any key to continue . . .
linq大神... 不过这lambda用的太吓人了吧 从来没想过 声明式编程的璀璨明珠linq ,可以写出可读性这么差的代码(当然,也是我linq水平一般) 个人感觉,如果中间产生了太多种类的匿名类型,则不太适合使用链式编程.
火拼阿三 2013-09-12
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
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)
            {
                Console.WriteLine(string.Join(", ", item));
            }
        }
    }
}
1, 2, 3 1, 2, 4 1, 3, 4 2, 3, 4 1, 2, 5 1, 3, 5 2, 3, 5 1, 4, 5 2, 4, 5 3, 4, 5 1, 2, 6 1, 3, 6 2, 3, 6 1, 4, 6 2, 4, 6 3, 4, 6 1, 5, 6 2, 5, 6 3, 5, 6 4, 5, 6 Press any key to continue . . .
大侠啊。能不能解释下你的代码,我看了一下午了还没看懂。。。真心佩服你。。
threenewbee 2013-08-16
  • 打赏
  • 举报
回复
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)
            {
                Console.WriteLine(string.Join(", ", item));
            }
        }
    }
}
1, 2, 3 1, 2, 4 1, 3, 4 2, 3, 4 1, 2, 5 1, 3, 5 2, 3, 5 1, 4, 5 2, 4, 5 3, 4, 5 1, 2, 6 1, 3, 6 2, 3, 6 1, 4, 6 2, 4, 6 3, 4, 6 1, 5, 6 2, 5, 6 3, 5, 6 4, 5, 6 Press any key to continue . . .
PaulyJiang 2013-08-16
  • 打赏
  • 举报
回复
N固定就用循环个,不固定就用递归
PaulyJiang 2013-08-16
  • 打赏
  • 举报
回复
N层循环吧 c
ly8261861 2013-08-16
  • 打赏
  • 举报
回复
/// <summary> /// 获取不重复随机数的字符串数组 /// </summary> /// <param name="num"></param> /// <param name="max"></param> /// <returns></returns> protected List<String> getRandomNum(int num,int max) { Random rd = new Random(); List<String> numArr = new List<String>(); while (numArr.Count < num) { int rdnum = rd.Next(1, max); if (!numArr.Contains(rdnum.ToString())) { numArr.Add(rdnum.ToString()); } } return numArr; } 功能应该类似,稍微改下就好
bdmh 2013-08-16
  • 打赏
  • 举报
回复

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧