Download presentation
Presentation is loading. Please wait.
Published byBridget Owen Modified over 8 years ago
1
Recursive
2
Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
3
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 8 ************************** n = ?
4
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 9 ************************** n = ?
5
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 10 ************************** n = 3
6
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 factorial(n) = ?
7
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 factorial(n) = ? Function: factorial Line: 2 ************************** n = 3
8
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 3 ************************** n = 3
9
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 5 ************************** n = 3
10
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 6 ************************** n = 3 Return n x …
11
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 6 ************************** n = 3 Return n x … Function: factorial Line: 2 ************************** n = 2
12
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 3 ************************** n = 3 Function: factorial Line: 3 ************************** n = 2
13
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 5 ************************** n = 3 Function: factorial Line: 5 ************************** n = 2
14
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 6 ************************** n = 3 Return n x … Function: factorial Line: 6 ************************** n = 2 Return n x …
15
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 6 ************************** n = 3 Return n x … Function: factorial Line: 6 ************************** n = 2 Return n x … Function: factorial Line: 2 ************************** n = 1
16
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 3 ************************** n = 3 Function: factorial Line: 3 ************************** n = 2 Function: factorial Line: 3 ************************** n = 1
17
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 3 ************************** n = 3 Function: factorial Line: 6 ************************** n = 2 Return n x … Function: factorial Line: 4 ************************** n = 1 Return 1
18
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 3 ************************** n = 3 Function: factorial Line: 6 ************************** n = 2 Return n x 1
19
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 Function: factorial Line: 6 ************************** n = 3 Return n x 2 Function: factorial Line: 6 ************************** n = 2 Return 2
20
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 factorial(n) = ? Function: factorial Line: 6 ************************** n = 3 Return 6
21
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 factorial(n) = ?
22
Recursive 1 2 3 4 5 6 7 8 9 10 11 12 13 #include int factorial(int n){ if(n<=1) return 1; else return n * factorial(n-1); } int main() { int n = 3; printf("%d! = %d\n", n, factorial(n) ); return 0; } Function: mian Line: 11 ************************** n = 3 factorial(n) = 6
23
Hanoi Tower
24
???
25
Hanoi Tower If you want to move n disks from SRC to DES…. Step 1: Move top n-1 from SRC to TMP Step 2: Move top 1 from SRC to DES Step 3: Move top n-1 from TMP to DES SRCTMPDES
26
Hanoi Tower How to Move top n-1 from SRC to TMP? This time, TMP become DES, DES become TMP. Step 1: Move top n-2 from SRC to TMP Step 2: Move top 1 from SRC to DES Step 3: Move top n-2 from TMP to DES SRCTMPDES
27
Hanoi Tower #include void hanoi(int n, int startRod, int tempRod, int targetRod){ if(n>0){ hanoi(n-1, startRod, targetRod, tempRod); printf(" 將 %d 盤從 %d 柱移動到 %d 柱 \n", n, startRod, targetRod); hanoi(n-1, tempRod, startRod, targetRod); } int main() { hanoi(3,1,2,3); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.