循环层数不确定,怎么办? 额,自行领悟何谓递归ps:就你的问题本身,请google“c# 笛卡尔积” 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 类似这个http://bbs.csdn.net/topics/390550326?page=1#post-395315031 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { int[][] data = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5 }, new int[] { 7, 8 }, new int[] { 9 } }; var query = data[0].Select(x => new int[] { x }); foreach (var item in data.Skip(1)) { query = query.SelectMany(x => item.Select(y => x.Concat(new int[] { y }).ToArray())); } foreach (var item in query) { Console.WriteLine(string.Join(", ", item)); } } }}1, 4, 7, 91, 4, 8, 91, 5, 7, 91, 5, 8, 92, 4, 7, 92, 4, 8, 92, 5, 7, 92, 5, 8, 93, 4, 7, 93, 4, 8, 93, 5, 7, 93, 5, 8, 9Press any key to continue . . . open System.Linq[<EntryPoint>]let main argv = begin let data = [| [| 1; 2; 3 |] [| 4; 5 |] [| 7; 8 |] [| 9 |] |] let mutable query = data.[0].Select(fun x -> [| x |]) for item in data.Skip(1) do query <- query.SelectMany(fun (x:int[]) -> item.Select(fun y -> x.Concat([| y |]).ToArray())) for item in query do printfn "%s" ((Array.fold(fun acc elem -> acc + elem.ToString() + ", ") "" (item)).TrimEnd(',', ' ')) 0 end1, 4, 7, 91, 4, 8, 91, 5, 7, 91, 5, 8, 92, 4, 7, 92, 4, 8, 92, 5, 7, 92, 5, 8, 93, 4, 7, 93, 4, 8, 93, 5, 7, 93, 5, 8, 9Press any key to continue . . . 转成递归,写法很简洁参考:http://jingyan.baidu.com/article/046a7b3efb6f5df9c27fa9ec.html copy代码运行 完全错误 c#调用com 求教正则式高手:如何过滤掉外部链接而保留本站链接? 视频监控的问题 要从字符串"123,-1,苏州XX公司"依次提取整数123,整数-1,字符串"苏州XX公司",如何写代码 C#如何开发QQ机器人 江湖救急!!!!!!! WebGame数据编辑器的实现思路 数据恢复的方法 Javascript getTime() 等同于c#里的那个函数?计算一个时间戳 在一个方案中,如何创建自己的顶级名称空间? c#中DataGridView导出Excel 一直有个问题困扰我,求解,求思路,谢谢!
http://bbs.csdn.net/topics/390550326?page=1#post-395315031
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[][] data = new int[][]
{
new int[] { 1, 2, 3 },
new int[] { 4, 5 },
new int[] { 7, 8 },
new int[] { 9 }
};
var query = data[0].Select(x => new int[] { x });
foreach (var item in data.Skip(1))
{
query = query.SelectMany(x => item.Select(y => x.Concat(new int[] { y }).ToArray()));
}
foreach (var item in query)
{
Console.WriteLine(string.Join(", ", item));
}
}
}
}1, 4, 7, 9
1, 4, 8, 9
1, 5, 7, 9
1, 5, 8, 9
2, 4, 7, 9
2, 4, 8, 9
2, 5, 7, 9
2, 5, 8, 9
3, 4, 7, 9
3, 4, 8, 9
3, 5, 7, 9
3, 5, 8, 9
Press any key to continue . . .
let main argv =
begin
let data =
[|
[| 1; 2; 3 |]
[| 4; 5 |]
[| 7; 8 |]
[| 9 |]
|]
let mutable query = data.[0].Select(fun x -> [| x |])
for item in data.Skip(1) do
query <- query.SelectMany(fun (x:int[]) -> item.Select(fun y -> x.Concat([| y |]).ToArray()))
for item in query do
printfn "%s" ((Array.fold(fun acc elem -> acc + elem.ToString() + ", ") "" (item)).TrimEnd(',', ' '))
0
end1, 4, 7, 9
1, 4, 8, 9
1, 5, 7, 9
1, 5, 8, 9
2, 4, 7, 9
2, 4, 8, 9
2, 5, 7, 9
2, 5, 8, 9
3, 4, 7, 9
3, 4, 8, 9
3, 5, 7, 9
3, 5, 8, 9
Press any key to continue . . .
参考:http://jingyan.baidu.com/article/046a7b3efb6f5df9c27fa9ec.html
copy代码运行 完全错误