2017-01-02 68 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