作者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