作者gawyfish (00)
站內TransCSI
標題Re: [問題] 程式
時間Mon May 16 22:58:58 2005
※ 引述《deepdish (要和數學裝熟..)》之銘言:
: 先整理一下比較容易看,我看的結果似乎會用到遞迴= ="
: ----------------------------------------------------
: int proc2(int *a)
: {
: int b;
: b = *a + 1;
: printf("b = %d\n", b);
: while(b <= 3)
: {
: b += proc2(&b);
: printf("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!!
: b = 2 // 第一次進入 proc2
: b = 3 // 第一次遞迴 proc2
: b = 4 // 第二次遞迴 proc2,由於大於 3,所以不會進入 while
: result b = 4 // the return value of b = 4
: b = 7 // 回到第一次遞迴 b = 3 + 4
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
這邊是執行哪一行阿? :
result b = 7 // the return value of b = 7
: b = 9 // 回到第一次進入 b = 2 + 7
: result b = 9 // the return value of b = 9
: n1 = 10 // 第一次回到 main,n1 = 1 + 9,但是 <= 30,所以繼續 while
^^^^^^^^^
為什麼回到main:
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: 203.135.73.232
推 deepdish:這位同學你有沒有學過程式設計阿…Orz 140.115.201.17 05/17
推 gawyfish:不好易思,問題沒問清楚~~~>,<麻煩版大了 61.62.95.39 05/17