作者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
1F:推 deepdish:这位同学你有没有学过程式设计阿…Orz 140.115.201.17 05/17
2F:推 gawyfish:不好易思,问题没问清楚~~~>,<麻烦版大了 61.62.95.39 05/17