2017-01-02 52 views
1

我有這些嵌套的循環:可變數量的頭部和尾部嵌套循環

void fun() { 

    int n = 5; 
    int c = 0; 

    for (int i = 0; i < n; i++) { 
     head_function(i, c); 
     for (int j = 0; j < n; j++) { 
      head_function(j, c); 
      for (int w = 0; w < n; w++) { 
       head_function(w, c); 
       for (int x = 0; x < n; x++) { 
        head_function(x, c); 

        c++; 
        body(c); 

        tail_function(x, c); 
       } 
       tail_function(w, c); 
      } 
      tail_function(j, c); 
     } 
     tail_function(i, c); 
    } 
} 

它並不真正的問題是什麼的頭部和尾部的功能來做,只要他們能夠跟蹤其指標我,j,w,x。

我想要的是有一個任意數量的嵌套for循環,而不是隻有四個。

我在這裏找到的其他解決方案並不適合我,因爲他們不包括頭部和尾部功能,我猜。

+3

使用遞歸方法和遞歸深度作爲參數。 –

+0

推薦使用遞歸,因爲大多數硬件專門處理堆棧內存,不需要的遞歸往往比其他方法使用更多的堆棧內存。您可以使用單個循環和一個數組來保留任意數量的索引。編碼比較複雜,但如果你的「深度」很大,最好用這種方法編碼。 – markspace

+1

@markspace現在這對你來說是不成熟的優化。一個人應該寫出清晰簡潔的代碼,遞歸在這裏是很自然的,如果OP是4深的,他可以在它成爲問題之前多加倍。您甚至可以通過增加VM堆棧內存來延長修復時間。 – Sylwester

回答

0

由於n似乎總是相同的,你可以嘗試(C現在是私人領域,n是嵌套的for循環數):

private int c; 

void loopAtDepth(int depth){ 
    if(depth == 0){ 
     c++; 
     body(c); 
    } else { 
     for(int i = 0; i < n; i++){ 
      head_function(i, c); 
      loopAtDepth(depth - 1) 
      tail_function(i, c); 
     } 
    } 
} 
1

這裏是一個骨架,讓你開始。隨意添加參數並根據您的需求更改簽名。

public void doStuffRecursively(int nTimes){ 
    doIt(nTimes); 
} 

void doIt(int depth){ 
    if(depth==0) 
     body(); 
    else{ 
     head(depth); 
     doIt(depth-1); 
     tail(depth); 
    } 
} 

void body(){} 

void head(int depth){} 
void tail(int depth){} 
1

這是一個迭代循環索引的單個數組的非遞歸版本。

你的代碼只是一個小竅門:最內層的循環與其他循環不同,只有它調用body()並增加通用計數器。我佔此通過我的循環內檢查的「內循環」,這是由可變loop是0

I所示還從5至3(max)變更了n,只是爲了降低輸出的大小。我認爲其餘的代碼非常簡單。

public class Test { 

    public static void main(String[] args) { 
     System.out.println("\n Loop 1\n"); 
     loop(1); 
     System.out.println("\n Loop 2\n"); 
     loop(2); 
     System.out.println("\n Loop 3\n"); 
     loop(3); 
    } 

    public static void loop(int depth) { 
     int[] indices = new int[depth]; 
     final int max = 3; 
     int count = 0; 
     for(int x = 0; x < depth - 1; x++) 
      head(x, count); 
     outer: 
     for(;;) { 
      for(int loop = 0; loop < indices.length; loop++) { 
       if(indices[loop] < max) { 
        head(indices[loop], count); 
        if(loop == 0) { 
         count++; 
         body(indices[loop], count); 
        } 
        tail(indices[loop], count); 
        indices[loop]++; 
        break; 
       } else { 
        if(loop == indices.length - 1) break outer; 
        indices[loop] = 0; 
       } 
      } 
     } 
    } 

    private static void head(int index, int counter) { 
     System.out.printf("head index=%d count=%d%n", index, counter); 
    } 
    private static void body(int index, int counter) { 
     System.out.printf("body index=%d count=%d%n", index, counter); 
    } 
    private static void tail(int index, int counter) { 
     System.out.printf("tail index=%d count=%d%n", index, counter); 
    } 

} 

的部分輸出(它得到相當長):

Loop 1 

head index=0 count=0 
body index=0 count=1 
tail index=0 count=1 
head index=1 count=1 
body index=1 count=2 
tail index=1 count=2 
head index=2 count=2 
body index=2 count=3 
tail index=2 count=3