2013-01-21 123 views
-5
for(a[9]=0;a[9]<16;a[9]++) 

for(a[8]=0;a[8]<16;a[8]++) 

for(a[7]=0;a[7]<16;a[7]++) 

for(a[6]=0;a[6]<16;a[6]++) 

for(a[5]=0;a[5]<16;a[5]++) 

for(a[4]=0;a[4]<16;a[4]++) 

for(a[3]=0;a[3]<16;a[3]++) 

for(a[2]=0;a[2]<16;a[2]++) 

for(a[1]=0;a[1]<16;a[1]++) 

for(a[0]=0;a[0]<16;a[0]++) 

i++; 
+3

你究竟想用這種深度嵌套循環來達到什麼目的? –

回答

2

下面的邏輯將幫助您將嵌套for循環轉換爲遞歸函數。但不要在代碼中不必要地添加這麼多的嵌套for循環。即使在編寫小程序時也需要考慮性能。

int i = 0; //make it as global variable 
... 
void func(int a[], int index) 
{ 
    for (a[index] = 0; a[index] < 16; a[index]++) 
    { 
     if (index != 0) 
     { 
      func(a, (index - 1)); 
     } 
     else 
     { 
      i++; 
     } 
    } 
}