【问题标题】:How to display the combination of some values in array? [duplicate]如何显示数组中某些值的组合? [复制]
【发布时间】:2013-08-08 22:59:19
【问题描述】:

例如,我有一个数组["Sam", "Mary", "John"]
我想显示从 3 中选择 2 的组合。
结果应该是:

[Sam, Mary]
[Sam, John]
[Mary, John] 

我研究了很多,但仍然不知道该怎么做。
当然,这个例子只包含 3 个人。
事实上,总人数会更大,例如15

这是我找到的:
Algorithm to return all combinations of k elements from n

What is a good way to implement choose notation in Java?

其中一些只是显示nCr的值,而不是给出组合。

【问题讨论】:

  • 在你的例子中,顺序很重要,但你说你想要组合(暗示顺序不)。是哪个?
  • 你总是想选择对,还是组合的大小会一直变化?
  • 您问题中的第一个链接 (Algorithm to return all combinations of k elements from n) 包含您问题的大量答案。
  • 不总是选择pair,它是我输入的变量
  • 使用recursion,卢克!

标签: java algorithm math


【解决方案1】:
    public static int width;

    public static void main(String [] args){

        String[] array = {"one", "two", "three", "four", "five"};

        width = 3;

        List<String> list = new ArrayList<String>();

        for (int i = 0; i < array.length; i++){
            method(array, list, i, 1, "[" + array[i]);
        }

        System.out.println(list);
    }


    public static void method(String[] array, List<String> list, int i, int depth, String string){

        if (depth == width){
            list.add(string + "]");
            return;
        }

        for (int j = i+1; j < array.length; j++){
            method(array, list, j, depth+1, string + ", " + array[j]);
        }
    }

【讨论】:

【解决方案2】:

打印给定字符串数组(名为array)的组合(nCr)的简单递归函数:

String[] array = {"Sam", "Mary", "John"};

public void function(int counter, String comb_Str, int r) {
        if (r == 0) {
            System.out.println(comb_Str);            
        } else {
            for (; counter < array.length; ++counter) {
                function(counter + 1, comb_Str + "  " + array[counter], r - 1);
            }
        }
    }

使用function(0, "", #r value#)调用

r 值应该是

【讨论】:

  • 我尝试运行但没有打印出来。当它运行第一个 for 循环时,它就退出了。
  • 在上面的例子中使用function(0,"",2); @jjLin
  • 第一个参数不是n,而是一个计数器/用来跟踪数组中的字符串
【解决方案3】:

这里有一些伪代码可以帮助您开始使用递归解决方案。列表比字符串数组更容易使用,因为您可以轻松更改它们的大小。此外,一旦你得到你的组合,你可以迭代它们以显示它们,但是你想要的。然而,虽然这是一个值得考虑的好问题,但组合的数量很快就会失控,因此如果您处理的结果不止少数,那么将它们全部显示给用户将成为一个坏主意...

/**
 * @param list The list to create all combos for
 * @param comboSize The size of the combo lists to build (e.g. 2 for 2 items combos)
 * @param startingIndex The starting index to consider (used mainly for recursion).  Set to 0 to consider all items.
 */
getAllCombos(list, comboSize, startingIndex){
    allCombos;

    itemsToConsider = list.length - startingIndex;
    if(itemsToConsider >= comboSize){
        allCombos = getAllCombos(list, comboSize, startingIndex + 1);

        entry = list[startingIndex];
        if(comboSize == 1){
            singleList;
            singleList.add(entry);
            allCombos.add(singleList);
        } else {
            subListCombos = getAllCombos(list, comboSize - 1, i+1);
            for(int i = 0; i < subListCombos.length; i++){
                subListCombo = subListCombos[i];
                subListCombo.add(entry);
                allCombos.add(subListCombo);
            }
        }
    }

    return allCombos;
}

【讨论】:

    【解决方案4】:

    这可能并不完美,但它应该能让你走上正轨。创建一个函数来获取每个元素的组合。然后你只需要遍历每个元素并在每个元素上调用你的函数。

    int num = 2; //Number of elements per combination
    
    for(int i=0; i <= (array.length - num); i++) {
        String comb = "[" + array[i];
        comb += getComb(i,num);
        comb += "]";
        println(comb);
    }
    
    String getComb(int i, int num) {
        int counter = 1;
        String s = "";
    
        while(counter < num) {
            s += ", " + array[i+counter];
            counter++;
        }
    
        return s;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多