求高手排列组合算法

99guo 2014-08-07 10:34:31
例如下面的例子:
数组1: "S1","S2","S3"
数组2: "B2"
数组3: "C1","C2"
得到结果 "S1,B2,C1","S1,B2,C2",“S2,B2,C1”,“S2,B2,C2”,"S3,B2,C1","S3,B2,C2"

我想得到上面的所有排列组合算法,求大神有没方法,如何做??
...全文
432 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
游离失所 2014-08-08
  • 打赏
  • 举报
回复
引用 10 楼 insus 的回复:
[quote=引用 9 楼 lyj224170707 的回复:] 都弱爆了。。看我的。。


      List<string> A = new List<string> { "S1", "S2", "S3" };
            List<string> B = new List<string> { "B2" };
            List<string> C = new List<string> { "C1", "C2" };
            List<List<string>> list = new List<List<string>>()
            {
                A,
                B,
                C
            };
            var result = list.Aggregate((thisCurrent, nextCurrent) => thisCurrent.SelectMany(x => nextCurrent.Select(y => y + x)).ToList());
            foreach (var temp in result)
            {
                Response.Write(temp + "<br/>");
            }
如果数组B的值有些变化: List<string> B = new List<string> { "B2" ,"B3"}; 再测试一下你的代码,不知所得的结果是否也是lz想要的结果? [/quote] C1B2S1 C2B2S1 C1B3S1 C2B3S1 C1B2S2 C2B2S2 C1B3S2 C2B3S2 C1B2S3 C2B2S3 C1B3S3 C2B3S3 这是加上B3后的运行结果
insus 2014-08-08
  • 打赏
  • 举报
回复
引用 9 楼 lyj224170707 的回复:
都弱爆了。。看我的。。


      List<string> A = new List<string> { "S1", "S2", "S3" };
            List<string> B = new List<string> { "B2" };
            List<string> C = new List<string> { "C1", "C2" };
            List<List<string>> list = new List<List<string>>()
            {
                A,
                B,
                C
            };
            var result = list.Aggregate((thisCurrent, nextCurrent) => thisCurrent.SelectMany(x => nextCurrent.Select(y => y + x)).ToList());
            foreach (var temp in result)
            {
                Response.Write(temp + "<br/>");
            }
如果数组B的值有些变化: List<string> B = new List<string> { "B2" ,"B3"}; 再测试一下你的代码,不知所得的结果是否也是lz想要的结果?
游离失所 2014-08-08
  • 打赏
  • 举报
回复
都弱爆了。。看我的。。


      List<string> A = new List<string> { "S1", "S2", "S3" };
            List<string> B = new List<string> { "B2" };
            List<string> C = new List<string> { "C1", "C2" };
            List<List<string>> list = new List<List<string>>()
            {
                A,
                B,
                C
            };
            var result = list.Aggregate((thisCurrent, nextCurrent) => thisCurrent.SelectMany(x => nextCurrent.Select(y => y + x)).ToList());
            foreach (var temp in result)
            {
                Response.Write(temp + "<br/>");
            }
uinatlex 2014-08-07
  • 打赏
  • 举报
回复
笛卡尔积 会不 初中该毕业了吧 List<string> A = new List<string> { "S1", "S2", "S3" }; List<string> B = new List<string> { "B2" }; List<string> C = new List<string> { "C1", "C2" }; IEnumerable<string> result = from a in A from b in B from c in C select a + b + c;
threenewbee 2014-08-07
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390572575 看我的回答
淡淡的活着 2014-08-07
  • 打赏
  • 举报
回复
套三循环······
uinatlex 2014-08-07
  • 打赏
  • 举报
回复
引用 3 楼 99guo 的回复:
[quote=引用 2 楼 uinatlex 的回复:] 笛卡尔积 会不 初中该毕业了吧 List<string> A = new List<string> { "S1", "S2", "S3" }; List<string> B = new List<string> { "B2" }; List<string> C = new List<string> { "C1", "C2" }; IEnumerable<string> result = from a in A from b in B from c in C select a + b + c;
这个方法是不错,但问题是数组不限于3层,可能N层。[/quote] 笛卡尔积就是不停的乘 你无限还不是乘,递归就完了

        List<string> RecursionLINQ(List<string[]> list, int startCount, List<string> result)
        {
            if (startCount < list.Count)
            {
                result = result.Count == 0 ?
                    (from b in list[startCount] select b).ToList() :
                    (from a in result from b in list[startCount] select a + b).ToList();

                result = RecursionLINQ(list, startCount + 1, result);
            }

            return result;
        }


            string[] strs1 = { "S1", "S2", "S3" };
            string[] strs2 = { "B2" };
            string[] strs3 = { "C1", "C2" };
            List<string[]> list = new List<string[]>();

            list.Add(strs1);
            list.Add(strs2);
            list.Add(strs3);

            List<string> result = new List<string>();

           result = RecursionLINQ(list, 0, result);
adawoo 2014-08-07
  • 打赏
  • 举报
回复
坐等大神!
yzf86211861 2014-08-07
  • 打赏
  • 举报
回复
看大神来表演了 。
99guo 2014-08-07
  • 打赏
  • 举报
回复
引用 2 楼 uinatlex 的回复:
笛卡尔积 会不 初中该毕业了吧 List<string> A = new List<string> { "S1", "S2", "S3" }; List<string> B = new List<string> { "B2" }; List<string> C = new List<string> { "C1", "C2" }; IEnumerable<string> result = from a in A from b in B from c in C select a + b + c;
这个方法是不错,但问题是数组不限于3层,可能N层。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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