2013-08-07 37 views
0

例如,我有一个数组["Sam", "Mary", "John"]
我想显示选择2出3
组合的结果应该是:如何显示数组中的某些值的组合?

[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公司的价值,而不是让出来的组合。

+0

在你的例子中,订单很重要,但你说你想要组合(暗示订单没有)。这是什么? – Daniel

+1

你是否总是想要选对,还是组合的大小将永远都是可变的? – Michelle

+0

您的问题中的第一个链接([算法从n返回k元素的所有组合](http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from -n))包含很多你的问题的答案。 – Carsten

回答

1

简单递归函数来给定的字符串数组(名为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值应为< = N值(数组长度)

+0

我尝试运行,但没有打印。当它运行第一个for循环时,它将退出。 – jjLin

+0

使用'function(0,“”,2);'为你的上例@jjLin –

+0

第一个参数不是'n',而是一个计数器/用于跟踪数组中的字符串 –

0

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

/** 
* @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; 
} 
2
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]); 
     } 
    } 
+0

你能否给出一些递归的解释,我不是很懂。 – jjLin

+1

看看这个问题:http://stackoverflow.com/questions/9199984/basic-java-recursion-method – Brinnis

0

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

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; 
} 
相关问题