我有一个类似这样的图层结构:
1.1
1.2
2.1
3.1
3.2
4.1
5.1
我想为每个项目输出所有可能的组合,但不包括订单高于自身的项目,订单是第一个数字,例如:
5.1 将具有以下组合:
(4.1, 3.1, 2.1, 1.1)
(4.1, 3.1, 2.1, 1.2)
(4.1, 3.2, 2.1, 1.1)
(4.1, 3.2, 2.1, 1.2)
4.1 将有:
(3.1, 2.1, 1.1)
(3.1, 2.1, 1.2)
(3.2, 2.1, 1.1)
(3.2, 2.1, 1.2)
我想用递归函数来做,我尝试了一些东西,但它不能像它应该的那样工作,这里是伪代码。
我从图层顺序开始,然后向下,直到它到达层数为1的图层。
function recursion (layer)
if layer.order > 1 do
for iterationLayer in allLayers do
if iterationLayer.order == (layer.order - 1) do
print iterationLayer.name
recursion iterationLayer
else
end of combination
此代码为我提供了类似 4.1 案例
的内容(3.1, 2.1, 1.1)
(1.2)
(3.2, 2.1, 1.1)
(1.2)
就像这样,因为当递归到达最后一个for循环时,它会从底部追溯到顶部,有没有人有解决方案?
答案 0 :(得分:1)
伪代码:
function recursionMain(level)
for iterationLayer in allLayers where (layer.level == level)
recursion (iterationLayer, new list)
function recursion(currentLayer, currentLayerList)
remainingLevelsList = get the distinct list of levels left to visit
if remainingLevelsList not empty
lowestRemamingLevel = get the lowest number from remainigLevelsList
for iterationLayer in allLayers where (layer.level == lowestRemainingLevel)
recursion (iterationLayer, currentLayerList + currentLayer)
else
print the list(combination) / end of combination
这是C#中的工作示例:
static void GetCombinationsR(int level)
{
foreach (Layer layer in layers.Where(l => l.Level == level))
{
GetCombinationsR(layer, new List<Layer>());
}
}
static void GetCombinationsR(Layer layer, List<Layer> currentLayers)
{
// Declaring new list so we don't loose the list of previous layers in currentLayers
List<Layer> currentLayers2 = new List<Layer>();
currentLayers2 = currentLayers2.Union(currentLayers).ToList();
currentLayers2.Add(layer);
// Getting the list of remaining levels because we are not certain
// if level 1 is the lowest or if some levels are skipped
List<int> remainingLevels = layers.Select(l => l.Level).Where(l => l < layer.Level).ToList();
if (remainingLevels.Count() > 0)
{
int firstLowerLevel = remainingLevels.OrderByDescending(l => l).First();
foreach (Layer layerByValue in layers.Where(l => l.Level == firstLowerLevel).ToArray())
{
GetCombinationsR(layerByValue, currentLayers2);
}
}
else
{
PrintResultList(currentLayers2);
}
}
static void PrintResultList(List<Layer> resultList)
{
StringBuilder sb = new StringBuilder();
foreach (Layer layer in resultList)
{
sb.Append(layer.Level).Append(": ").Append(layer.Value).Append(" -> ");
}
sb = sb.Remove(sb.Length - 4, 4);
Console.WriteLine(sb.ToString());
}
答案 1 :(得分:0)
也许你需要这样的东西:
config.stopBubbling = true
lombok.addGeneratedAnnotation = false
lombok.accessors.chain = false
lombok.anyConstructor.suppressConstructorProperties = true
Delphi示例(非最佳,我们可以更快地搜索并提前断开循环)
function recursion (layer, combination)
if layer = 0
print combination
else
for (i = 0; i < layer.itemcount; i++)
recursion(layer - 1, combination + layer.item[i])
recursion(layer-1, empty[])