intmain() { int n = 10; printf("Fibonacci(%d) = %d\n", n, fibonacci(n)); return0; }
2. 阶乘计算
阶乘的定义为:
n! = n * (n-1) * (n-2) * … * 1
0! = 1
C语言代码:
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h>
intfactorial(int n) { if (n == 0) return1; // 0! = 1 return n * factorial(n - 1); }
intmain() { int n = 5; printf("%d! = %d\n", n, factorial(n)); return0; }
3. 汉诺塔问题
汉诺塔问题是经典的递归问题,目标是将所有盘子从源柱子移动到目标柱子,遵循以下规则:
每次只能移动一个盘子。
不能将较大的盘子放在较小的盘子上。
C语言代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<stdio.h>
voidhanoi(int n, char from, char to, char aux) { if (n == 1) { printf("Move disk 1 from %c to %c\n", from, to); return; } hanoi(n - 1, from, aux, to); // 递归移动n-1个盘子到辅助柱 printf("Move disk %d from %c to %c\n", n, from, to); hanoi(n - 1, aux, to, from); // 递归将n-1个盘子从辅助柱移动到目标柱 }
intmain() { int n = 3; // 盘子的数量 hanoi(n, 'A', 'C', 'B'); // A为源柱,B为辅助柱,C为目标柱 return0; }
intbinary_search(int arr[], int low, int high, int target) { if (low > high) return-1; // 未找到 int mid = (low + high) / 2; if (arr[mid] == target) return mid; if (arr[mid] > target) return binary_search(arr, low, mid - 1, target); return binary_search(arr, mid + 1, high, target); }
intmain() { int arr[] = {1, 3, 5, 7, 9, 11, 13}; int n = sizeof(arr) / sizeof(arr[0]); int target = 7; int result = binary_search(arr, 0, n - 1, target); if (result != -1) printf("Element found at index %d\n", result); else printf("Element not found\n"); return0; }
// 递归计算最大子数组和 intmax_cross_sum(int arr[], int l, int m, int h) { int sum = 0, left_sum = -2147483648; for (int i = m; i >= l; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; }
sum = 0, right_sum = -2147483648; for (int i = m + 1; i <= h; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; }
return left_sum + right_sum; }
intmax_subarray_sum(int arr[], int l, int h) { if (l == h) return arr[l];
int m = (l + h) / 2; return fmax(fmax(max_subarray_sum(arr, l, m), max_subarray_sum(arr, m + 1, h)), max_cross_sum(arr, l, m, h)); }
intmain() { int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int n = sizeof(arr) / sizeof(arr[0]); int max_sum = max_subarray_sum(arr, 0, n - 1); printf("Maximum subarray sum is %d\n", max_sum); return0; }
voidcombine(int arr[], int data[], int start, int end, int index, int r) { if (index == r) { for (int j = 0; j < r; j++) { printf("%d ", data[j]); } printf("\n"); return; }
for (int i = start; i <= end && end - i + 1 >= r - index; i++) { data[index] = arr[i]; combine(arr, data, i + 1, end, index + 1, r); } }
intmain() { int arr[] = {1, 2, 3, 4}; int r = 2; // 组合的大小 int data[r]; int n = sizeof(arr) / sizeof(arr[0]); combine(arr, data, 0, n - 1, 0, r); return0; }
intbinary_search(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binary_search(arr, l, mid - 1, x); return binary_search(arr, mid + 1, r, x); } return-1; // 如果找不到 }
intmain() { int arr[] = {2, 3, 4, 10, 40}; int n = sizeof(arr) / sizeof(arr[0]); int x = 10; int result = binary_search(arr, 0, n - 1, x); if (result == -1) printf("Element not found\n"); else printf("Element is present at index %d\n", result); return0; }
int main() { char text[] = "aab"; char pattern[] = "c*a*b"; if (is_match(text, pattern)) { printf("Pattern matches the text\n"); } else { printf("Pattern does not match the text\n"); } return 0; }