给定2个数组Array1 = {a,b,c...n},Array2 = {10,20,15....x}如何生成所有可能的组合作为字符串a(i)b(j)c(k)n(p)
其中
1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x
Run Code Online (Sandbox Code Playgroud)
如:
a1 b1 c1 .... n1
a1 b1 c1..... n2
......
......
a10 b20 c15 nx (last combination)
Run Code Online (Sandbox Code Playgroud)
所以在所有组合的总数=元素的产品 array2 =
(10 X 20 X 15 X ..X x)
类似于笛卡尔积,其中第二个数组定义第一个数组中每个元素的上限.
固定数字的示例,
Array x = [a,b,c]
Array y = [3,2,4]
Run Code Online (Sandbox Code Playgroud)
所以我们将有3*2*4 = 24种组合.结果应该是:
a1 b1 c1
a1 b1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在c#.net中构建一个应用程序
这里我有两个相同大小的单维数组.例如,我有矩阵M和N,如下面的结构:
M[0] M[1] M[2] M[3] M[4]
N[0]
N[1]
N[2]
N[3]
N[4]
Run Code Online (Sandbox Code Playgroud)
在这里,我将M [0] ......和N [0] ......的值分配给它们,以便得到如下矩阵:
5 6 4 8
4
8
7
2
Run Code Online (Sandbox Code Playgroud)
注意:我将此值设置为动态生成.我已经成功直到这一步.
但我喜欢以这种格式将值存储在2x2矩阵中的另一个数组(可能是锯齿状数组或其他数组)中:
A[0] A[1]
B[0] 5 4 (this is the values of M[0] and N[0])
B[1] 6 4 (this is the values of M[1] and N[0])
..............
B[4] 5 8 (this is the values of M[0] and N[1])
Run Code Online (Sandbox Code Playgroud)
当N [0]的第一行完成时,它必须继续下一行.我只需要一些如何在C#中实现它?
可能重复:
生成所有可能的组合
是否有一个很好的LINQ方式来做笛卡尔积?
如何在没有显式嵌套循环的情况下生成N个元素的组合,每个元素的供应量为2
我有一个列表列表,我想迭代所有可能的组合,我从每个内部列表中选择一个元素.如果我在编译时知道有多少列表,这是非常简单的,但是如果我事先不知道会有多少列表,我怎么能这样做呢?
如果我有三个列表(如果我知道,在编译时,将会有三个列表),并且我想要从三个列表中的每个列表中选择一个元素的所有组合,我可以轻松地使用LINQ查询:
var list1 = new[] { 1, 2 };
var list2 = new[] { 3, 4 };
var list3 = new[] { 5, 6 };
var combinations = from item1 in list1
from item2 in list2
from item3 in list3
select new[] { item1, item2, item3 };
// Results:
// {1, 3, 5}
// {1, 3, 6}
// {1, 4, 5}
// {1, 4, 6}
// {2, 3, 5}
// {2, 3, 6} …Run Code Online (Sandbox Code Playgroud) 我有一个类别表,它被设置为允许无限数量的子类别级别.我想模仿以下内容:

应该澄清的是,子类别可以有子类别.例如父母 - > 1级 - > 2级 - > 3级等
我的类别表有两列,CategoryName和ParentID.
在为产品分配正确的类别时,将使用此列表框.
我怎么写这个?
编辑
为了回应thedugas我必须修改你的答案,以适应我的情况.我发现了一些需要修复的错误,但下面是最终的工作解决方案.
protected void Page_Load(object sender, EventArgs e)
{
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
var c = db.Categories.Select(x => x);
List<Category> categories = new List<Category>();
foreach (var n in c)
{
categories.Add(new Category()
{
categoryID = n.categoryID,
title = n.title,
parentID = n.parentID,
isVisible = n.isVisible
});
}
List<string> xx = new List<string>();
foreach (Category cat in categories) …Run Code Online (Sandbox Code Playgroud) 我有
List<List<string>> AllSimilarWordsLists { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想从这些单词生成字符串,以便没有字符串重复,这里重复意味着每个字符串必须包含唯一的单词
例如,如果曾经生成'你好吗'那么'你是不是应该在结果中考虑'.
我可以有任意数量的清单
例如
List1 List2 List3 List4 List5
word11 word21 word21 word21 word51
word12 word22 word22 word22 word52
word13 word23 word23 word23 word53
word14 word24 word24 word24 word54
word15 word25 word25 word25 word55
Run Code Online (Sandbox Code Playgroud)
这些列表将添加到AllSimilarWordsLists中.我想使用笛卡尔积生成字符串列表.已经找到了这个,但是这个解决方案有固定数量的列表,任何有想法的人.
就像是:
forelement (element G_Element, Grid)
{
Grid[G_Element.dim1, G_Element.dim2] =
new clsGridElement(G_Element.dim1, G_Element.dim2);
}
Run Code Online (Sandbox Code Playgroud)
代替
for (int X = 0; X < GridWidth; X++)
for (int Y = 0; Y < GridHeight; Y++)
Grid[X, Y] = new clsGridElement(X, Y);
Run Code Online (Sandbox Code Playgroud)
如果某些东西不是天生存在的,那么可以创造出什么?
蒂姆,谢谢
c# ×5
linq ×2
arrays ×1
asp.net ×1
combinations ×1
iteration ×1
linq-to-sql ×1
permutation ×1
recursion ×1