本文介绍了如何$的组合使用,而闭环P $ pcalculate有效的数字,而不是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于数据中心它们DC1,DC2,DC3和机器的列表中,H1,H2,H3,H4如下所述的清单 -

Given List of datacenters which are dc1, dc2, dc3 and list of machines, h1, h2, h3, h4 as mentioned below -

Datacenters = dc1, dc2, dc3
Machines = h1, h2, h3, h4

我要生成以下组合只 -

I want to generate below combinations only -

a)  {dc1=h1, dc3=h3, dc2=h2}

b)  {dc1=h2, dc3=h4, dc2=h3}

c)  {dc1=h3, dc3=h1, dc2=h4}

d)  {dc1=h4, dc3=h2, dc2=h1}

在每遍每一个数据中心应该得到备用机/主机。他们不应该得到同样的机器。例如,在 A 如上图所示 - DC1获得H1 DC2得到H2 DC3得到H3 因此,所有的机器都为每个数据中心不同。而在如图 B 第二遍 - 现在 DC1得到H2 (监守DC1已经拿到H1在第一遍), DC2了H3 (bcoz DC2已经有了H2在第一遍)和 DC3了H4 (bcoz DC3已经得到了H3在第一遍)等等。

Each datacenter in the each pass should get alternate machines/hosts. They should not get same machines. For example in a as shown above - dc1 gets h1, dc2 gets h2, dc3 gets h3 so all the machines are different for each datacenters. And in the second pass as shown in b - now dc1 gets h2 (becuase dc1 already got h1 in the first pass), dc2 got h3 (bcoz dc2 already got h2 in the first pass), and dc3 got h4(bcoz dc3 already got h3 in the first pass) and etc etc.

和一个例子 - 如果我只有三台主机,那么下面结合我应该得到的只有 -

And one more example - if I have only three hosts, then below combination I am supposed to get only -

Datacenters = dc1, dc2, dc3
Machines = h1, h2, h3

{dc1=h1, dc3=h3, dc2=h2}
{dc1=h2, dc3=h1, dc2=h3}
{dc1=h3, dc3=h2, dc2=h1}

于是我想出了低于code的工作完全正常 -

So I came up with the below code which works perfectly fine -

public class DataCenterMapping {

    public static void main(String[] args) {

    DatacenterMachineMapping dcm = new DatacenterMachineMapping(Arrays.asList("dc1", "dc2", "dc3"), Arrays.asList(
        "h1", "h2", "h3", "h4"));

        // is there any way to avoid while loop here?
        while (true) {
            Map<String, String> coloHost = dcm.getDatacenterMachineMapping();
            System.out.println(coloHost);
            for (Map.Entry<String, String> entry : coloHost.entrySet()) {

            }
        }
    }
}

class DatacenterMachineMapping {

    private boolean firstCall = true;
    private int hostListIndex = 0;
    private List<String> datacenterList, hostList;
    private Map<String, Set<String>> dataCenterHostsMap = new HashMap<String, Set<String>>();

    public DatacenterMachineMapping(List<String> datacenterList, List<String> hostList) {
    this.datacenterList = datacenterList;
    this.hostList = hostList;
    }

    public Map<String, String> getDatacenterMachineMapping() {
    Map<String, String> datacenterMachineMapping = new HashMap<String, String>();
    if (!firstCall) {
        if (hostListIndex <= 0) {
        hostListIndex = hostList.size();
        }
        hostListIndex--;
    } else {
        firstCall = false;
    }
    for (String datacenter : datacenterList) {
        if (hostListIndex == hostList.size()) {
        hostListIndex = 0;
        }
        if (addDataCenterHost(datacenter, hostList.get(hostListIndex))) {
        datacenterMachineMapping.put(datacenter, hostList.get(hostListIndex++));
        }
    }
    hostListIndex--;
    return datacenterMachineMapping;
    }

    private boolean addDataCenterHost(String datacenter, String host) {
    Set<String> dataCenterHostSet = dataCenterHostsMap.get(datacenter);
    if (dataCenterHostSet == null) {
        dataCenterHostSet = new HashSet<String>();
        dataCenterHostsMap.put(datacenter, dataCenterHostSet);
    }
    return dataCenterHostSet.add(host);
    }
}

问题陈述: -

唯一的问题是我有一个while循环,这将继续一直在运行,

The only problem is I have a while loop which will keep on running always,

有没有什么办法可以使用​​whil​​e循环?precalculate有效组合的数量,而不是

Is there any way I can precalculate the number of valid combinations instead of using while loop?

推荐答案

你说的是数学。答案为(n选k),其中n是机的数量,k是数据中心的数目。

You're talking math. The answer is (n choose k), where n is the number of machines, and k is the number of datacenters.

原因是以下几点:顺序并不重要,所以我们假设数据中心始终排列顺序相同。对于第一个数据中心,我们可以挑选 N 机中的任何一个。对于第二,我们可以挑选的机器中的任何一个,除了所述一个拾取之前,从而 N *(N-1)。接下来的数据中心将会导致 N *(N-1)*(N-2)可能出现的情况。

The reason is the following: The ordering doesn't really matter, so we'll assume that the Datacenters are always arranged in the same order. For the first data center, we can pick any one of the n machines. For the second, we can pick any one of the machines, except for the one picked before, thus n * (n-1). The next data center will lead to n * (n-1) * (n-2) possible situations.

因此​​,如果你有10台机器,和4个数据中心,你会:

Thus, if you had 10 machines, and 4 datacenters, you would have:

10 * 9 * 8 * 7 不可能性的组合。

这里更多信息: http://en.wikipedia.org/wiki/Combination

如果你想有一个功能做的工作适合你,它是在Apache的公地:http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/ArithmeticUtils.html#binomialCoefficientDouble%28int,%20int%29

If you want a function to do the work for you , it is in the Apache commons: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/ArithmeticUtils.html#binomialCoefficientDouble%28int,%20int%29

不过,如果你确实想要的生成的这些组合,那么你需要一个for循环。

However, if you are actually wanting to generate those combinations, then you need a for loop.

这篇关于如何$的组合使用,而闭环P $ pcalculate有效的数字,而不是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:54