gpt4 book ai didi

c# - 获取所有可能的参数组合

转载 作者:太空狗 更新时间:2023-10-30 00:34:17 25 4
gpt4 key购买 nike

我有一个包含可能值的参数列表:

// Definition of a parameter
public class prmMatrix
{
public string Name { get; set; }
public List<string> PossibleValues { get; set; }

public prmMatrix(string name, List<string> values)
{
Name = name;
PossibleValues = values;
}
}

//[...]

// List of params
List<prmMatrix> lstParams = new List<prmMatrix>();

lstParams.Add(new prmMatrix("Option A", new List<string>() { "Yes", "No" }));
lstParams.Add(new prmMatrix("Option B", new List<string>() { "Positive", "Negative" }));

我想要所有可能的参数组合,例如:

[Option A:Yes][Option B:Positive]
[Option A:Yes][Option B:Negative]
[Option A:No][Option B:Positive]
[Option A:No][Option B:Negative]

C# 中最好的方法是什么?

最佳答案

使用递归这很容易:

void ImplCombinations(List<prmMatrix> plist, string built, int depth, List<string> results)
{
if (depth >= plist.Count()) {
results.Add(built);
return;
}

prmMatrix next = plist[depth];
built += "[" + next.Name + ":";
foreach (var option in next.PossibleValues)
ImplCombinations(plist, built + option + "]", depth + 1, results);
}

List<string> GetCombinations(List<prmMatrix> plist)
{
List<string> results = new List<string>();
ImplCombinations(plist, "", 0, results);
return results;
}

关于c# - 获取所有可能的参数组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com