先整理一下比較容易看,我看的結果似乎會用到遞迴= ="
這個遞迴程式既然這麼難看懂,我們就稍微修改一下,比較容易懂
----------------------------------------------------
int proc2(int *a)
{
int b;
b = *a + 1;
printf("b = %d\n", b);
while(b <= 3)
{
b += proc2(&b);
printf("while b = %d\n", b);
}
printf("result b = %d\n", b);
return (b);
}
void main(void)
{
int n1;
printf("an easy test!!\n");
n1 = 1;
while(n1 <= 30)
{
n1 += proc2(&n1);
printf("n1 = %d\n", n1);
}
}
ans: an easy test!!// 先執行 main
b = 2 // 因為 n1 = 1 <= 30,進入 while,第一次進入 proc2
b = 3 // 第一次遞迴 proc2
b = 4 // 第二次遞迴 proc2,由於大於 3,所以不會進入 while
result b = 4 // the return value of b = 4
while b = 7 // 回到第一次遞迴 b = 3 + 4
result b = 7 // the return value of b = 7
while b = 9 // 回到第一次進入 b = 2 + 7
result b = 9 // the return value of b = 9
n1 = 10 // 第一次回到 main,n1 = 1 + 9,但是 <= 30,所以繼續 while
b = 11 // 第二次進入 proc2,由於 > 3,所以不進入 while
result b = 11 // the return value of b = 11
n1 = 21 // 第二次回到 main,n1 = 10 + 11,還是 <=30,繼續 while
b = 22 // 第三次進入 proc2,因為 > 3,所以不進入 while
result b = 22 // the return value of b = 22
n1 = 43 // 第三次回到 main,n1 = 21 + 22,已經 > 30,結束 while
--
歡迎大家一起加入Intel Philanthropic Peer-to-Peer Program !!!
這項「英特爾慈善『點對點連線』計畫」旨在經由網際網路,把數百萬部個人電腦連結
起來,加速研發治療白血球過多症(血癌)的藥物,從而把新藥上市的需要時間縮短約
一半。對本計畫有興趣者,可以到http://www.grid.org/download/gold/download.htm
網站,下載該程式。
一旦一批資料處理完畢,下次電腦連接上網際網路時,不論經由寬頻或撥接,電腦便會
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.201.17