简体   繁体   English

Java内部循环

[英]Java inner for-loop

I have to find all possibilities to distribute n things to k containers. 我必须找到将n个事物分发到k个容器的所有可能性。 The containers all should have a different size so I made k inner for-loops for counting every possibility. 容器的大小都应该不同,所以我制作了k个内部for循环来计算每种可能性。 Sorry for the bad explanation, but my english is not that good. 对不起,我的英语解释不好。

Example code that works for 3 Containers: 适用于3个容器的示例代码:

 for (int i = 0; i < container[0]; i++)
     for (int j = 0; j < container[1]; j++)
         for (int k = 0; k < container[2]; k++)
             if ((i + j + k) == n)
                 Possibilities++; 

Now i need to know how to make k for loops so that it works for 2 and for 10. 现在我需要知道如何使k为循环,以便它适用于2和10。

Thanks 谢谢

I am assuming that containers holds the size of each container. 我假设containers容纳每个容器的大小。 Perhaps the simplest solution would be to just set the size of any containers you don't use to zero. 也许最简单的解决方案是将不使用的任何容器的大小设置为零。 Then you can have (say) 10 nested loops but if there are only 2 containers then set all the sizes above 2 to zero. 然后,您可以有(说)10个嵌套循环,但是如果只有2个容器,则将2以上的所有大小都设置为零。

However nested loops are not really the best way to handle this. 但是,嵌套循环实际上并不是处理此问题的最佳方法。 This is likely to be a good use for recursion. 这很可能是递归的好用法。

private int combinationCount(int[] containerSizes, int from, int total) {
    if (total == 0 || from == containerSizes.length - 1)
        return 1;
    int combinations = 0;
    for (int i = 0; i <= Math.min(total, containerSizes[from]); i++)
        combination += combinationCount(containerSizes, from + 1, total - i);
    return combinations;
}

This would be called with combinationCount(containers, 0, n) . 这可以通过combinationCount(containers, 0, n)来调用。 You could remove the from argument altogether by either copying the array in each recursive call or passing a List and then a sublist in the recursive calls. 您可以通过在每次递归调用中复制数组,或者在递归调用中传递一个List然后是一个子列表, from完全删除from变量。

Here is my test code for your information: 这是我的测试代码,供您参考:

System.out.println(combinationCount(IntStream.range(10, 30).toArray(), 0, 10));

Which returns 20030010 哪个返回20030010

I'll explain how this works: 我将解释其工作原理:

if (total == 0 || from == containerSizes.length - 1)
    return 1;

If the total of the remaining containers is zero then there's only 1 combination because all the remaining containers must be empty. 如果剩余容器的总数为零,则只有1个组合,因为所有剩余容器必须为空。 Similarly if there's only one container left there's only 1 combination because it must have all the remaining items. 同样,如果只剩下一个容器,则只有1个组合,因为它必须具有所有剩余的物品。

int combinations = 0;
for (int i = 0; i <= Math.min(total, containerSizes[from]); i++)
    combination += combinationCount(containerSizes, from + 1, total - i);
return combinations;

The current container might contain anything from zero to all the items. 当前容器可能包含从零到所有项目的任何内容。 So iterate through those and total up all the combinations for the remaining containers that add up to the target minus the items in the current container. 因此,对那些对象进行迭代并总计剩余容器的所有组合,这些组合加到目标中减去当前容器中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM