2013-08-07 39 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; 
} 
相關問題