作者gawyfish (00)
站內TransCSI
標題Re: [問題] 程式
時間Tue May 17 12:48:48 2005
※ 引述《deepdish (要和數學裝熟..)》之銘言:
: 先整理一下比較容易看,我看的結果似乎會用到遞迴= ="
: 這個遞迴程式既然這麼難看懂,我們就稍微修改一下,比較容易懂
: ----------------------------------------------------
: int proc2(int *a)
: {
: int b;
: b = *a + 1;
: printf("b = %d\n", b);
: while(b <= 3)---->(2)
: {
: b += proc2(&b);
: printf("while b = %d\n", b);--->(1)
: }
: printf("result b = %d\n", b);
: return (b); b=4的時後為什麼還會進去(1)阿 不是經過 (2)的判斷
: }
: 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
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.62.95.39