Re: [討論] 新手問題請教

看板C_and_CPP作者 (永遠睡不著 @@)時間10年前 (2013/12/20 16:40), 編輯推噓4(402)
留言6則, 6人參與, 最新討論串1/1
先知道當 n < 3 時 展開 e(n) 後會變成 e(++n) printf("%d\n", n) e(++n) 例如 n == 0 時 展開 e(0) 會變為: e(1) // n 由 0 加為 1 printf("%d\n", 1) e(2) // n 由 1 加為 2 當 n >= 3 時, 展開 e(n) 後會變為空, 也就是甚麼事情都不做 了解這兩個情況後就逐層展開: main: e(0) [展開第一層] e(0) main: 展開 e(0): e(1) printf("%d\n", 1) e(2) [展開第二層] e(1), e(2) main: 展開 e(0): 展開 e(1): e(2) printf("%d\n", 2) e(3) printf("%d\n", 1) 展開 e(2): e(3) printf("%d\n", 3) e(4) [展開第三層] e(2), e(3), e(3), e(4) main: 展開 e(0): 展開 e(1): 展開 e(2): e(3) printf("%d\n", 3) e(4) printf("%d\n", 2) 展開 e(3): // 此時 n < 3 為非, 展開後為空 printf("%d\n", 1) 展開 e(2): 展開 e(3): // 此時 n < 3 為非, 展開後為空 printf("%d\n", 3) 展開 e(4): // 此時 n < 3 為非, 展開後為空 [展開第四層] e(3), e(4) main: 展開 e(0): 展開 e(1): 展開 e(2): 展開 e(3): // 此時 n < 3 為非, 展開後為空 printf("%d\n", 3) 展開 e(4): // 此時 n < 3 為非, 展開後為空 printf("%d\n", 2) 展開 e(3): printf("%d\n", 1) 展開 e(2): 展開 e(3): printf("%d\n", 3) 展開 e(4): 全部都展開後, 觀察 printf 順序 printf("%d\n", 3) printf("%d\n", 2) printf("%d\n", 1) printf("%d\n", 3) 雖然遞迴比較好的方式是理解遞迴關係. 不過既然是初學者, 應該要試著練習展開. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.29.148 ※ 編輯: Feis 來自: 140.112.29.148 (12/20 16:48)

12/20 23:58, , 1F
好人
12/20 23:58, 1F

12/21 00:10, , 2F
哇靠還上色XD
12/21 00:10, 2F

12/21 01:19, , 3F
非常感謝大大的解釋,我瞭解了
12/21 01:19, 3F

12/21 01:47, , 4F
佛.......
12/21 01:47, 4F

12/21 13:54, , 5F
簡單明瞭
12/21 13:54, 5F

12/21 15:17, , 6F
人真好
12/21 15:17, 6F
文章代碼(AID): #1Ij09XuC (C_and_CPP)