Hàm đệ quy
Lập trình nâng cao
Hàm đệ quy
2
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long number = 9;
cout << number << "! = " << factorial(number);
return 0;
}
N! = N * (N-1)!
Hàm đệ quy
3
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
Hàm đệ quy
4
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
5
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đỉnh stack
main()
6
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
7
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
factorial(3)
x = 3
8
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
factorial(3)
x = 3
factorial(2)
x = 2
9
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
factorial(3)
x = 3
factorial(2)
x = 2
factorial(1)
x = 1
10
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
Return 1
main()
factorial(4)
x = 4
factorial(3)
x = 3
factorial(2)
x = 2
11
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
factorial(3)
x = 3
Return 2
12
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đáy stack
Đỉnh stack
main()
factorial(4)
x = 4
Return 6
13
long factorial(long x) {
if (x > 1)
return (x * factorial(x-1));
else
return 1;
}
int main() {
long res = factorial(4);
return 0;
}
Đỉnh stack
main()
res = 24
Return 24
Tìm kiếm nhị phân đệ quy
14
Tìm kiếm nhị phân đệ quy
15
Tìm kiếm nhị phân đệ quy
16
Tìm kiếm nhị phân đệ quy
a[mid] == key: tìm thấy
low>high: không tìm thấy
17
Tìm kiếm nhị phân đệ quy
18
int search(int key, int a[], int low, int high) {
if (low > high) return -1;
int mid = (low + high) / 2;
if (a[mid] == key) return mid;
if (a[mid] > key)
return search(key, a, low, mid-1);
return search(key, a, mid+1, high);
}
Đọc thêm
19
Công thức đệ quy
20
21
22
23
24
25
Thuật toán chuyển n đĩa
26
Thuật toán chuyển n đĩa
27
Thuật toán chuyển n đĩa
28
Thuật toán chuyển n đĩa
29
move(n, source, dest):
move(n-1, source, temp)
output(“move disc n from source to dest”);
move(n-1, temp, dest)
void move(int n, int source, int dest) {
temp = other(source, dest);
move(n - 1, source, temp);
output(“ move disc n from ‘source’ to ‘dest’ ”);
move(n - 1, temp, dest);
}
30
move(n, source, dest):
move(n-1, source, temp)
output(“move disc n from source to dest”);
move(n-1, temp, dest)
Thuật toán hoàn chỉnh
void move(int n, int source, int dest)
{
if (n < 1) return; // basic case
temp = other(source, dest);
move(n - 1, source, temp);
output(“ move disc n from ‘source’ to ‘dest’ ”);
move(n - 1, temp, dest);
}
31
Duyệt hoán vị
32
Duyệt hoán vị (abc)
tách a
tách b
tách c
Duyệt (bc)
Duyệt (ac)
Duyệt (ab)
tách b
tách c
Duyệt (c)
Duyệt (b)
Tìm được hoán vị abc
Tìm được hoán vị acb
…
Duyệt hoán vị
P(abcdef) danh sách tất cả các hoán vị của abcdef
P(abcdef) = a + P(bcdef)
b + P(acdef)
…..
permutation(s[], lo, hi): liệt kê tất cả hoán vị
if (lo == hi) { output(s) ; return;}
for i: lo-> hi {
swap(s, lo, i); // tách lấy phần tử đứng đầu
permutation(s, lo+1, hi) // đệ quy phần còn lại
swap(s, lo, i); // quay lui về như cũ để thử cách khác
}
33
Duyệt tổ hợp
C(N) là tập của các tập con của [1…N]
Thì
C(N) = C(N-1) + ([N] + s), với mọi s thuộc C(N-1)
34
Duyệt tổ hợp
C(N) = C(N-1) + ([N] + s), với mọi s thuộc C(N-1)
Combination(“abc”, 3) -> in ra 8 kết quả
void combination(k) {
if (k<1) {
output(); return;
}
member[k] = true;
combination(k-1);
member[k] = false;
combination(k-1);
}
35