#include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE (1) #define FALSE (0) typedef int Status; /** * -- 1. Recursive interpretation: * like a Russian doll, you open one layer at a time from the outermost layer until the last layer is opened and there is nothing left to open (end condition of recursion), and then layer by layer from the inside out. */ /** * -- 2. Three elements of recursion: * recursive termination conditions * Recursive termination processing * Extract repeated logic, reduce the size of the problem */ ** * -- 3. */ * int recursion(large) {if(end condition) {end_function(); } else {// continue recursion(small); }} */ /** * -- 4. The problem is defined recursively (Fibonacci function, factorial...). * (2). The solution of the problem is recursive (some problems can only be solved by recursive method, for example, Hannotta problem...). * (3). Data structures are recursive (operations on linked lists, trees, etc., including tree traversal, tree depth...). . */ /** * factorial */ int recursion_factorial(int n) {if (n == 1) {return 1; } else { return n * recursion_factorial(n-1); }} / int recursion_fibonacc(int pre, int curr, int n) {if (n < 0) {return -1; } if (n == 1) { return pre; } else if (n == 2) { return curr; } else { return recursion_fibonacc(curr, pre+curr, n - 1); }} /** * recursion_yanghui_triangle(int at_x, int at_x, int at_y) { if (at_y <= at_x && at_y >= 0) { if (at_y == 0 || at_x == at_y) { return 1; } else {return recursion_yanghui_triangle(at_x-1, AT_Y-1) + yanghui_triangle(at_x-1, AT_Y); }} /** * recursion_search(int *array, int low, int high, int target) { if(low <= high) { int mid = (low+high) / 2; if(array[mid] == target) { return mid+1; } else if (array[mid] > target) { return recursion_search(array, low, mid-1, target); } else { return recursion_search(array, mid+1, high, target); } } return -1; } int main(int argc, char const *argv[]) { int num_factorial = 0; int num_fibonacc = 0; int num_yanghui = 0; int num_search = 0; num_factorial = recursion_factorial(3); num_fibonacc = recursion_fibonacc(1, 1, 6); num_yanghui = recursion_yanghui_triangle(3, 2); int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; num_search = recursion_search(array, 0, 9, 3); printf("num_factorial=%d\n", num_factorial); printf("num_fibonacc=%d\n", num_fibonacc); printf("num_yanghui=%d\n", num_yanghui); printf("num_search=%d\n", num_search); return 0; }Copy the code