/// <summary>
/// 全组合算法
/// </summary>
/// <param name="dimvalue"></param>
/// <param name="result"></param>
/// <param name="layer"></param>
/// <param name="curstring"></param>
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]);
s1.Append("&");
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]);
}
}
}
/// 全组合算法
/// </summary>
/// <param name="dimvalue"></param>
/// <param name="result"></param>
/// <param name="layer"></param>
/// <param name="curstring"></param>
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]);
s1.Append("&");
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]);
}
}
}
}
调用实例
var lstSource = new List<List<string>>
{
new List<string>() { "A","B","C"},
new List<string>() { "D","E","F"},
new List<string>() { "G","H","I"},
};
List<string> result = new List<string>();
Descartes.run(lstSource, result, 0, "");