2011-08-25 37 views
2

我有嵌套循環嵌套(r =)3次。每個循環運行(n =)5次。受控嵌套循環

for (i=0; i<n; i++) 
{ 
    for (j=0; j<n; j++) 
    { 
     for (k=0; k<n; k++) 
     // 
    } 
} 

但是我們如何在運行時動態地進行嵌套。假設我們知道它應該嵌套r次。每個循環運行n次。我認爲像遞歸,但它無限期地去。

funloop (int r) 
{ 
    for (int i = 0; i < n; i++) 
    { 
      // 
      if (r < 3) 
       funloop (r++); 
      else 
       return; 
     } 
} 

請讓我知道如何做到這一點?我無法在網上找到來源。

+0

你可以發佈第二種情況下的確切代碼嗎?你擁有的邏輯是接近正確的,不應該無限循環,但是如果沒有看到實際的代碼,我不能提供對潛在錯誤的更詳細解釋。 – templatetypedef

+0

你的代碼是完全錯誤的,因爲你在第一個例子中使用了3次i(現在是i,j,k),第二個例子中的if語句之後是兩個表達式,而不是嵌套在一個塊中。所以還有一個懸念。如果您發佈可編譯代碼,則不會發生此類錯誤。第二個例子是imho仍然是錯誤的,因爲註釋標記了真正的代碼應該在哪裏,但是這應該在else塊中(然後它也需要花括號)。 –

回答

1

最簡單的方法是把它崩一個for循環:

for(i=0; i<pow(n, r); i++) { 
} 

但是這可以使它很難進入循環計數器,如果你需要他們,但是可以在數學上做。

int c = i % n; 

你可以有這種計數器陣列並與類似的公式確定的值,或者你可以增加他們,在需要時,如:

例如,最裏面的循環計數變量的值由下式給出
void iterate(int r, int n) { 
    int i, rc, *c = malloc(sizeof(int) * r); 

    memset(c, 0, sizeof(int) * r); 
    for(i = 0; i < pow(n, r); i++) { 

    // code here, using loop counters in the 'c' array, where c[0] is counter 
    // for the outer loop, and c[r - 1] is the counter for the innermost loop 

    // update the counters 
    rc = r; 
    while(rc > 0) { 
     rc--; 
     c[rc]++; 
     if(c[rc] == n) { 
     c[rc] = 0; 
     } else { 
     break; 
     } 
    } 
    } 
    free(c); 
} 
+0

噢,我正要回答的是什麼:但是有一點,我們的代碼非常相似。 –

+0

謝謝。我最初的問題是編碼重複排列。最後讓它工作。 – John

+1

使用'pow'作爲循環的上界看起來很奇怪。一般來說,我認爲應該更加註意循環變量的類型。這些很容易溢出。我想,使用'uintmax_t'會更合適。 –

3

如果您不知道靜態遞歸的深度,最常見的方法是使用遞歸來表示循環。例如,假設你需要有d級的循環嵌套,都需要迭代k次。然後你可以實現,使用遞歸這種形式:

void RecursivelyNestIterations(unsigned d, unsigned k) { 
    /* Base case: If the depth is zero, we don't need to iterate. */ 
    if (d == 0) return; 

    /* Recursive step: If we need to loop d times, loop once, calling the 
    * function recursively to have it loop d - 1 times. 
    */ 
    for (unsigned i = 0; i < k; ++i) { 
     /* Recurse by looping d - 1 times using the same number of iterations. */ 
     RecursivelyNestIterations(d - 1, k); 
    } 
} 

希望這有助於!

+0

我有一個代碼來打印nPr選擇:我將如何將其更改爲遞歸模型?'的#define N 4 \t 的#define R 2 無效的主要(無效){ \t \t INT X [R]; \t int * list =(int *)malloc(sizeof(int)* n); \t \t \t int count = 0,i,j,k; \t for(i = 0; i John

+0

@ John-如果您需要該代碼的幫助,那麼您應該將其作爲單獨的問題發佈。另外,我認爲你應該嘗試爲自己做一個練習。嘗試遞歸思考 - 如何讓問題變小?您如何將較小的解決方案整合到更大的解決方案中? – templatetypedef

+0

其實我從一開始就一直在嘗試遞歸解決方案,但發現它很難並且離開它。也許我會更努力。 :)如果我再次失敗,我會發布它。 – John

0

只需在循環體中調用if (r) funloop(r-1);即可。

0
#include <stdlib.h> 
#include <stdio.h> 

static int n = 3; 

void _funloop(int cur,int total) 
{ 
    if(cur!=total) 
    { 
     for(int cnt=0;cnt!=n;++cnt) 
     { 
      fprintf(stdout,"%d::%d\n",cur,cnt); 
     } 

     _funloop(cur+1,total); 
    } 
} 

void funloop(int total) 
{ 
    _funloop(0,total); 
} 

int main() 
{ 
    funloop(10); 

    return 0; 
} 
0

不使用遞歸的溶液在此提示討論 [鏈接] http://www.codeproject.com/Tips/759707/Generating-dynamically-nested-loops 該代碼是在C++,需要#爲包括和限定語句

include <iostream> 
define MAXROWS 9 
define MAXVALUES 9 

using namespace std; 
char display[] = {'1','2','3','4','5','6','7','8','9'}; 

int main() { 

int arrs[MAXROWS]; // represent the different variables in the for loops 

bool status = false; 

for (int r=0;r<MAXROWS;r++) 
    arrs[r] = 0; // Initialize values 

while (!status) { 

    int total = 0; 
    // calculate total for exit condition 
    for (int r=0;r<MAXROWS;r++) 
     total +=arrs[r]; 
    // test for exit condition 
    if (total == (MAXVALUES-1)*MAXROWS) 
     status = true; 

    // printing 
    for (int r=0;r<MAXROWS;r++) 
     cout << display[arrs[r]]; // print(arrs[r]) 
    cout << endl; // print(endline) 

    // increment loop variables 
     bool change = true; 
    int r = MAXROWS-1; // start from innermost loop 
    while (change && r>=0) { 
     // increment the innermost variable and check if spill overs 
     if (++arrs[r] > MAXVALUES-1) {   
      arrs[r] = 0; // reintialize loop variable 
      // Change the upper variable by one 
      // We need to increment the immediate upper level loop by one 
      change = true; 
     } 
     else 
      change = false; // Stop as there the upper levels of the loop are unaffected 

     // We can perform any inner loop calculation here arrs[r] 

     r=r-1; // move to upper level of the loop 

    } 

}