[英]How to combine elements of a matrix without using the same row more than once each time
我的目标是结合这些元素(每个字母代表一个不同的字符串)
A;B;F;...
X;C;D;...
P;O;K;...
...
以任何可能的方式在文本框中输入内容,而无需重复并且不合并同一行的元素。 点表示延续。 所以如果矩阵只是
A;B
X;C
结果应为AX AC BX BC。 如果它是
A;B;F
X;C;D
P;O;K
结果将是AXP AXO AXK ACP ACO ACK ADP ADO ADK BXP ....
我找到了一种算法
private void buildAllCombinationsRecursive<TSource>(IList<TSource> i_targetList, IList<TSource> i_sourceList, int i_currentPos)
{
if (i_currentPos == i_targetList.Count)
{
string combination = "";
for (int i = 0; i < i_targetList.Count; i++)
{
combination += i_targetList[i] + " ";
}
Console.WriteLine(combination);
return;
}
for (int i = 0; i < i_sourceList.Count; i++)
{
i_targetList[i_currentPos] = i_sourceList[i];
this.buildAllCombinationsRecursive(i_targetList, i_sourceList, i_currentPos + 1);
}
}
但是它创建了我不需要的ABC CBA等。
查看Eric Lippert的C#笛卡尔积递归 。
您的每个“矩阵”行都将是递归步骤中的一个序列。
累加器函数将是字符串连接。 即使出于性能原因,您也可以选择StringBuilder.Append 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.