111,090
社区成员




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)
{
List<List<string>> lists = new List<List<string>>()
{
new List<string>() { "张三", "李四" },
new List<string>() { "你", "我" },
new List<string>() { "人" },
new List<string>() { "食物", "材料" },
new List<string>() { "x", "y" },
new List<string>() { "d", "s" }
};
List<IEnumerable<List<string>>> query = SelectNFromM(lists, 3);
foreach (IEnumerable<List<string>> item in query)
{
IEnumerable<IEnumerable<string>> query1 = CartesianProduct<string>(item.ToList());
foreach (var item1 in query1)
{
Console.WriteLine(string.Join("&", item1));
}
}
}
static List<IEnumerable<T>> SelectNFromM<T>(List<T> data, int n)
{
int[] are = Enumerable.Range(0, data.Count).ToArray();
var result = are.Select(x => new int[] { x });
for (int i = 0; i < n - 1; i++)
{
result = result.SelectMany(x => are.Where(y => y.CompareTo(x.First()) < 0).Select(y => new int[] { y }.Concat(x).ToArray()));
}
return result.Select(x => x.Select(y => data[y])).ToList();
}
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(List<List<T>> source)
{
List<T[]> result = source[0].Select(x => new T[] { x }).ToList();
for (int i = 1; i < source.Count; i++)
{
if (i == source.Count) break;
result = result.SelectMany(x => source[i].Select(y => x.Concat(new T[] { y }).ToArray())).ToList();
}
return result;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class Descartes
{
public static void run(List<List<string>> dimvalue, List<string> result, int layer, string curstring)
{
if (layer < dimvalue.Count - 1)
{
if (dimvalue[layer].Count == 0)
run(dimvalue, result, layer + 1, curstring);
else
{
for (int i = 0; i < dimvalue[layer].Count; i++)
{
StringBuilder s1 = new StringBuilder();
s1.Append(curstring);
s1.Append(dimvalue[layer][i]);
run(dimvalue, result, layer + 1, s1.ToString());
}
}
}
else if (layer == dimvalue.Count - 1)
{
if (dimvalue[layer].Count == 0) result.Add(curstring);
else
{
for (int i = 0; i < dimvalue[layer].Count; i++)
{
result.Add(curstring + dimvalue[layer][i]);
}
}
}
}
}