2011-04-30 24 views
1

所以我有這個數組只包含唯一的數字,其中索引0處的數字最低,數組最末尾的數字最高。遞增一個唯一的數字序列

E.g. [1,2,3,4]

現在我每增加1次後面的數字。但是當任何數字達到一定的高度時,它應該增加左邊的數字。

E.g.比方說,最大高度是8。

[1,2,3,8] - > [1,2,4,5]

現在直到我在這裏的代碼工作。但是當最後2個數字達到最大高度時,它不會再增加第三個數字。

E.G. [1,2,7,8] - > [1,3,4,5]

我寫的代碼是遞歸的。

//Position is the index in the array of which element should be incremented by 1 
public int[] increaseArray(int[] index, int maxIndex, int position) { 
     int tmp = index[position]; 
     if (tmp < maxIndex) { 
      index[position] = tmp + 1; 
      return index; 
     } else { 
      if (positie != 0 && index[position - 1] + 2 <= maxIndex) { 
       index[position] = index[position - 1] + 2; 
       return increaseArray(index, maxIndex, position - 1); 
      } else { 
       return null; 
      } 
     } 
    } 

EDIT 1:

所得陣列只包含唯一的號碼,所以是INT [2]刷爆至7在這裏。

另外我編輯了代碼。我感覺,我幾乎沒有雖然最後人數仍在竊聽......

public int[] increaseIndex(int[] index, int maxIndex, int position) { 
    int tmp = index[position]; 
    if (tmp < maxIndex + position - 2) { 
     index[position] = tmp + 1; 
     return index; 
    } else { 
     if (position > 0) { 
          //The following line of code is the problem... 
      index[position] = index[position - 1] + 2; 
      return increaseIndex(index, maxIndex, position - 1); 
     } else { 
      return null; 
     } 
    } 
} 

編輯2:

真的接近現在。我像說的那樣修正了maxIndex。現在有一個小錯誤,應該增加2個以上的數字。

代碼

public int[] increaseIndex(int[] index, int maxIndex, int position) { 
    int size = index.length; 
    int tmp = index[position]; 
    if (tmp < maxIndex - (size-position-1)) { 
     index[position] = tmp + 1; 
     return index; 
    } else { 
     if (position > 0) { 
      //The following line is the problem i think... 
      index[position] = index[position - 1] + 2; 
      return increaseIndex(index, maxIndex, position - 1); 
     } else { 
      return null; 
     } 
    } 
} 

當我使用以下執行代碼這會給我下面的輸出例如與maxIndex 8

int[] index = new int[] {1,2,3,4}; 
index = increaseIndex(index, row.length - 1, k - 2); 
    while (index != null) { 
     printArray(index); 
     index = increaseIndex(index, row.length - 1, k - 2); 
    } 

[1, 2, 3, 4] 
[1, 2, 3, 5] 
[1, 2, 3, 6] 
[1, 2, 3, 7] 
[1, 2, 3, 8] 
[1, 2, 4, 5] 
[1, 2, 4, 6] 
[1, 2, 4, 7] 
[1, 2, 4, 8] 
[1, 2, 5, 6] 
[1, 2, 5, 7] 
[1, 2, 5, 8] 
[1, 2, 6, 7] 
[1, 2, 6, 8] 
[1, 2, 7, 8] 
[1, 3, 4, 9] //wrong 
[1, 3, 5, 6] 
[1, 3, 5, 7] 
[1, 3, 5, 8] 
[1, 3, 6, 7] 
[1, 3, 6, 8] 
[1, 3, 7, 8] 
[1, 4, 5, 9] //wrong 
[1, 4, 6, 7] 
[1, 4, 6, 8] 
[1, 4, 7, 8] 
[1, 5, 6, 9] //wrong 
[1, 5, 7, 8] 
[1, 6, 7, 9] //wrong 
[2, 3, 8, 9] //wrong 
[2, 4, 5, 10]//wrong 
[2, 4, 6, 7] 
[2, 4, 6, 8] 
[2, 4, 7, 8] 
[2, 5, 6, 9] //wrong 
[2, 5, 7, 8] 
[2, 6, 7, 9] //wrong 
[3, 4, 8, 9] //wrong 
[3, 5, 6, 10]//wrong 
[3, 5, 7, 8] 
[3, 6, 7, 9] //wrong 
[4, 5, 8, 9] //wrong 
[4, 6, 7, 10]//wrong 
[5, 6, 8, 9] //wrong 
+0

沒有結果數組需要獨特的號碼以及?還是他們都應該最大程度的發揮在8點? (在這個例子中)。 含義... if int [3] == 8,maxed out,int [2] == 7 maxed,還是應該== 8? – 2011-04-30 15:06:55

+0

您的MaxIndex應該在每個位置都有所不同。 – 2011-04-30 15:07:20

+0

「c#」和「算法」標籤添加了'unique'和'increment'標籤。 – 2011-04-30 15:24:57

回答

0

這是一個不遞歸的不同方法。它會嘗試所有組合並過濾掉那些不是唯一的組合。

這種方法可以很容易地適應各種要求。

public static void main(String... args) { 
    uniqueCombinations(4, 8); 
} 

private static void uniqueCombinations(int depth, int maxValue) { 
    int[] ints = new int[depth]; 
    long combinations = (long) Math.pow(maxValue, depth); 
    LOOP: 
    for (long l = 0; l < combinations; l++) { 
     long l2 = l; 
     // create a combination. 
     for (int i = ints.length - 1; i >= 0; i--) { 
      ints[i] = (int) (l2 % maxValue + 1); 
      l2 /= maxValue; 
     } 
     // check the combination. 
     for (int i = 0; i < ints.length; i++) 
      for (int j = i + 1; j < ints.length; j++) 
       if (ints[i] == ints[j]) continue LOOP; 
     // print a result. 
     System.out.println(Arrays.toString(ints)); 
    } 
} 

打印

[1, 2, 3, 4] 
[1, 2, 3, 5] 
[1, 2, 3, 6] 
[1, 2, 3, 7] 
[1, 2, 3, 8] 
[1, 2, 4, 3] 
..... 
[8, 7, 5, 6] 
[8, 7, 6, 1] 
[8, 7, 6, 2] 
[8, 7, 6, 3] 
[8, 7, 6, 4] 
[8, 7, 6, 5] 
0

這在python:

def choose_iter(elements, length): 
    for i in xrange(len(elements)): 
     if length == 1: 
      yield (elements[i],) 
     else: 
      for next in choose_iter(elements[i+1:len(elements)], length-1): 
       yield (elements[i],) + next 

輸出:

>>> for res in choose_iter([1, 2, 3, 4, 5, 6, 7, 8], 4): 
     print res 


(1, 2, 3, 4) 
(1, 2, 3, 5) 
(1, 2, 3, 6) 
(1, 2, 3, 7) 
(1, 2, 3, 8) 
(1, 2, 4, 5) 
(1, 2, 4, 6) 
(1, 2, 4, 7) 
(1, 2, 4, 8) 
(1, 2, 5, 6) 
(1, 2, 5, 7) 
(1, 2, 5, 8) 
(1, 2, 6, 7) 
(1, 2, 6, 8) 
(1, 2, 7, 8) 
(1, 3, 4, 5) 
(1, 3, 4, 6) 
(1, 3, 4, 7) 
(1, 3, 4, 8) 
(1, 3, 5, 6) 
(1, 3, 5, 7) 
(1, 3, 5, 8) 
(1, 3, 6, 7) 
(1, 3, 6, 8) 
(1, 3, 7, 8) 
(1, 4, 5, 6) 
(1, 4, 5, 7) 
(1, 4, 5, 8) 
(1, 4, 6, 7) 
(1, 4, 6, 8) 
(1, 4, 7, 8) 
(1, 5, 6, 7) 
(1, 5, 6, 8) 
(1, 5, 7, 8) 
(1, 6, 7, 8) 
(2, 3, 4, 5) 
(2, 3, 4, 6) 
(2, 3, 4, 7) 
(2, 3, 4, 8) 
(2, 3, 5, 6) 
(2, 3, 5, 7) 
(2, 3, 5, 8) 
(2, 3, 6, 7) 
(2, 3, 6, 8) 
(2, 3, 7, 8) 
(2, 4, 5, 6) 
(2, 4, 5, 7) 
(2, 4, 5, 8) 
(2, 4, 6, 7) 
(2, 4, 6, 8) 
(2, 4, 7, 8) 
(2, 5, 6, 7) 
(2, 5, 6, 8) 
(2, 5, 7, 8) 
(2, 6, 7, 8) 
(3, 4, 5, 6) 
(3, 4, 5, 7) 
(3, 4, 5, 8) 
(3, 4, 6, 7) 
(3, 4, 6, 8) 
(3, 4, 7, 8) 
(3, 5, 6, 7) 
(3, 5, 6, 8) 
(3, 5, 7, 8) 
(3, 6, 7, 8) 
(4, 5, 6, 7) 
(4, 5, 6, 8) 
(4, 5, 7, 8) 
(4, 6, 7, 8) 
(5, 6, 7, 8) 

我把它在這裏提出一個不同的方法 - 正確的,而不是想着遞增的數字,認爲它是爲了撿拾元件。實施將是非常不同的,但它是一種完全不同的方法。

0

我看不出有任何理由在這裏使用(或不使用)遞歸,我不會,如果我是你。

問題是,你沒有定義當一個數字已經在maxIndex時應該發生什麼。

我會建議一個解決方案,但是你沒有定義你的程序在這種情況下應該如何表現。

它在這一點上所做的是無論如何增加它。

你可能想沿着東西線跳過這已經在maxIndex(或以上)的所有位置:

while (position >= 0 && index[position] >= maxIndex) position--; 
if (position == -1) return; //in case all positions are already at maximum value