mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * Update PrintUtil.hpp * fix(codes/c): Fix some errors of cmake build * feat(codes/c): Add hashing_search.c * styles(codes/c): Modify function description * styles(codes/c): Modify binary_search.c code style * fix(codes/c): Fix the problem in binary_tree_bfs.c and the problem that the memory is not released. * feat: Add preorder_traversal_i_compact.c * feat(codes/c): Add head_sort.c * feat(codes/c): Add bucket_sort.c * feat(codes/c): Add binary_search_edge.c * fix(codes/c): Add programs that are not managed by cmake (c code) * feat(codes/c): Add selection_sort.c * style(codes/c): Change swap in selection_sort.c to `selectionSort` * styles(codes/c): Change style. * fix(codes/c): Fix some formatting errors and temporarily remove backtracking chapters * fix(codes/c): Fix space_complexity.c build error. * feat(codes/c): Add array_binary_tree.c * feat(code/c): Update push_back and pop_back in vector.h * styles(codes/c): Adjust format. * feat(codes/c): Add `interation.c ` `recursion.c` `simple_hash.c` `binary_search_edge.c` `binary_search_insertion.c` in C codes. --------- Co-authored-by: Yudong Jin <krahets@163.com>
		
			
				
	
	
		
			56 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/**
 | 
						|
 * File: recursion.c
 | 
						|
 * Created Time: 2023-09-09
 | 
						|
 * Author: Gonglja (glj0@outlook.com)
 | 
						|
 */
 | 
						|
 | 
						|
#include "../utils/common.h"
 | 
						|
 | 
						|
/* 递归 */
 | 
						|
int recur(int n) {
 | 
						|
    // 终止条件
 | 
						|
    if (n == 1)
 | 
						|
        return 1;
 | 
						|
    // 递:递归调用
 | 
						|
    int res = recur(n - 1);
 | 
						|
    // 归:返回结果
 | 
						|
    return n + res;
 | 
						|
}
 | 
						|
 | 
						|
/* 尾递归 */
 | 
						|
int tailRecur(int n, int res) {
 | 
						|
    // 终止条件
 | 
						|
    if (n == 0)
 | 
						|
        return res;
 | 
						|
    // 尾递归调用
 | 
						|
    return tailRecur(n - 1, res + n);
 | 
						|
}
 | 
						|
 | 
						|
/* 斐波那契数列:递归 */
 | 
						|
int fib(int n) {
 | 
						|
    // 终止条件 f(1) = 0, f(2) = 1
 | 
						|
    if (n == 1 || n == 2)
 | 
						|
        return n - 1;
 | 
						|
    // 递归调用 f(n) = f(n-1) + f(n-2)
 | 
						|
    int res = fib(n - 1) + fib(n - 2);
 | 
						|
    // 返回结果 f(n)
 | 
						|
    return res;
 | 
						|
}
 | 
						|
 | 
						|
/* Driver Code */
 | 
						|
int main() {
 | 
						|
    int n = 5;
 | 
						|
    int res;
 | 
						|
 | 
						|
    res = recur(n);
 | 
						|
    printf("\n递归函数的求和结果 res = %d\n", res);
 | 
						|
 | 
						|
    res = tailRecur(n, 0);
 | 
						|
    printf("\n尾递归函数的求和结果 res = %d\n", res);
 | 
						|
 | 
						|
    res = fib(n);
 | 
						|
    printf("\n斐波那契数列的第 %d 项为 %d\n", n, res);
 | 
						|
 | 
						|
    return 0;
 | 
						|
}
 |