1 / 30

アルゴリズムとデータ構造 補足資料 3-2 「関数 fact の再帰呼出」

アルゴリズムとデータ構造 補足資料 3-2 「関数 fact の再帰呼出」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. 戻り値が int 型の関数 fact. 仮引数 n. int 型 (32bit ). int fact ( int n ) { int fn; printf (“Call :fact(%d)<br>”, n); if(n&gt;0) fn=n* fact (n-1); else fn=1; printf (“ Return:fact (%d)==%d n”,n,fn );

Télécharger la présentation

アルゴリズムとデータ構造 補足資料 3-2 「関数 fact の再帰呼出」

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. アルゴリズムとデータ構造補足資料3-2「関数factの再帰呼出」アルゴリズムとデータ構造補足資料3-2「関数factの再帰呼出」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志

  2. 戻り値がint型の関数 fact 仮引数 n int型(32bit) intfact(int n) { int fn; printf(“Call :fact(%d)\n”, n); if(n>0) fn=n*fact(n-1); else fn=1; printf(“Return:fact(%d)==%d\n”,n,fn); return fn; } (自動)変数 fn int型(32bit) printf(“Call :fact(%d)\n”, n); no yes n>0 ? fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn); return int型(32bit)

  3. 戻り値がint型の関数 main 仮引数: なし void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, k, fact(k)); return int型(32bit)

  4. 戻り値がint型の関数 main 仮引数: なし mainから実行開始! void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, fact(3)); fact(3)を呼び出し return int型(32bit)

  5. fact(3) n 3 fn printf(“Call :fact(%d)\n”, n); no n>0 ? yes fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) return

  6. fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); no 3>0 ? yes fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) return

  7. fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=3*fact(3-1); printf(“Return:fact(%d)==%d\n”,n,fn) return

  8. fact(3) fact(2) n n 2 3 fn fn printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, n); fact(2) no 3>0 ? n>0 ? yes yes fn=1; fn=3*fact(2); fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return

  9. fact(3) fact(2) n n 2 3 fn fn printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); fact(2) no 3>0 ? 2>0 ? yes yes fn=1; fn=3*fact(2); fn=2*fact(2-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return

  10. fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, n); printf(“Call :fact(%d)\n”, 3); fact(2) no 2>0 ? 3>0 ? n>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=2*fact(1); fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return

  11. fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); fact(2) no 2>0 ? 3>0 ? 1>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=2*fact(1); fn=1*fact(1-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return

  12. fact(3) fact(0) fact(2) fact(1) n n n n 2 3 0 1 fn fn fn fn printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, n); printf(“Call :fact(%d)\n”, 2); fact(2) no 2>0 ? 1>0 ? n>0 ? 3>0 ? yes yes yes yes fact(1) fn=1; fn=3*fact(2); fn=n*fact(n-1); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)

  13. fact(3) fact(0) fact(2) fact(1) n n n n 2 3 0 1 fn fn fn fn printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 2); fact(2) no 2>0 ? 1>0 ? 0>0 ? 3>0 ? yes yes yes yes fact(1) fn=1; fn=3*fact(2); fn=n*fact(n-1); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)

  14. fact(3) fact(0) fact(1) fact(2) n n n n 3 0 1 2 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 1); fact(2) no 0>0 ? 1>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)

  15. fact(3) fact(0) fact(2) fact(1) n n n n 2 0 1 3 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 1); fact(2) no 3>0 ? 1>0 ? 0>0 ? 2>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) return return return return 1 fact(0)

  16. fact(3) fact(0) fact(2) fact(1) n n n n 0 3 2 1 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); fact(2) no 1>0 ? 0>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=1*fact(0); fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return 1 fact(0)=1

  17. fact(3) fact(0) fact(2) fact(1) n n n n 0 3 2 1 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); fact(2) no 1>0 ? 0>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=1* 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return 1 fact(0)=1

  18. fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn 1 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); fact(2) 2>0 ? 3>0 ? 1>0 ? yes yes yes fact(1) fn= 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return

  19. fact(3) fact(1) fact(2) n n n 1 3 2 fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 1>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn= 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,1,1) printf(“Return:fact(%d)==%d\n”,n,fn) return return return 1

  20. fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 1>0 ? 2>0 ? 3>0 ? yes yes yes fn=3*fact(2); fn= 1; fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,1,1) printf(“Return:fact(%d)==%d\n”,n,fn) fact(1)=1 return return return 1

  21. fact(3) fact(2) n n 3 2 fn fn 2 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 2>0 ? 3>0 ? yes yes fn=3*fact(2); fn=2; printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return

  22. fact(3) fact(2) n n 3 2 fn fn 2 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); fact(2) 3>0 ? 2>0 ? yes yes fn=3*fact(2); fn=2; printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,2,2) return return 2

  23. fact(3) fact(2) n n 2 3 fn fn 2 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); 2>0 ? 3>0 ? yes yes fn=2; fn=3*fact(2); fact(2)=2 printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,2,2) return return 2

  24. fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=3*2; printf(“Return:fact(%d)==%d\n”,n,fn) return

  25. fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,n,fn) return

  26. fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,3,6) return 6

  27. fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,3,6) return 6 main関数へfact(3)=6

  28. 戻り値がint型の関数 main 仮引数: なし main関数に戻る void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, fact(3)); fact(3)を呼び出し fact関数からfact(3)=6 return int型(32bit)

  29. 戻り値がint型の関数 main 仮引数: なし main関数に戻る void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, 6); fact関数からfact(3)=6 return int型(32bit)

  30. 戻り値がint型の関数 main 仮引数: なし void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, 6); return 0 正常終了 int型(32bit)

More Related