设为首页收藏本站

爱吱声

 找回密码
 注册
搜索
查看: 1709|回复: 3
打印 上一主题 下一主题

[科技前沿] 突然想到让deepseek来解释一下递归

[复制链接]
  • TA的每日心情
    开心
    2025-9-8 05:08
  • 签到天数: 3 天

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 : `& I' a# d' F: V

    : q$ @0 l* ?. o5 N解释的不错
    + Y. R' }! J( q! M; a
    ' x' m& P+ V  ?) Q$ c/ d递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    - J/ w  i1 W+ V. Y
    9 g4 x8 _" m3 A& p4 m0 ^% k6 u/ ` 关键要素9 H" E: A, ~, K; s/ ^& d) W% k
    1. **基线条件(Base Case)**
    $ f+ \! B( R0 b5 D   - 递归终止的条件,防止无限循环
    / Y) i8 i* Y- K( m5 T! r   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    9 Q8 u* _3 M$ C  u  _
    * Z( {: w" E7 X9 k! p2. **递归条件(Recursive Case)**4 d" q8 G. @4 C; ]5 ?- G" N6 g( S3 A
       - 将原问题分解为更小的子问题  l; b7 g6 \1 g6 r& e
       - 例如:n! = n × (n-1)!/ E. \0 h/ f: ]9 ^  G
    9 \" i2 Z( \7 a( L+ K9 A
    经典示例:计算阶乘
    0 q* n* F7 g3 Spython9 E! N! z( M! J* s2 e; {2 o/ o5 M& b
    def factorial(n):# ^3 G" K  y. U9 M( g
        if n == 0:        # 基线条件
    , N$ i. Y1 O( c9 t  }, H, |; W# Z        return 1- D" _: \' f+ H7 ]1 |: j
        else:             # 递归条件
    ( o) a. U2 c" _" b0 @! f  B        return n * factorial(n-1)
    % T8 x: ~+ l/ D7 X; V: W/ a6 M执行过程(以计算 3! 为例):. P7 M; Y1 }$ w
    factorial(3)
    : Y, F0 U& x" J% J+ C' P3 * factorial(2)6 x0 E! W" I7 a: A& x5 F
    3 * (2 * factorial(1))- W: h0 X2 _6 h, b
    3 * (2 * (1 * factorial(0)))
    " ^$ k# I, h* e  z0 X2 a5 w1 b) ~3 * (2 * (1 * 1)) = 6
    $ ]% z6 `9 O7 j  A; m1 i
    . [4 f; }9 F/ U 递归思维要点
    * g/ S% z, K# k  s% b& m9 E, ?1. **信任递归**:假设子问题已经解决,专注当前层逻辑; m. k7 o7 D7 E& g4 H
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)) }3 \& t3 J8 n( i; W; S
    3. **递推过程**:不断向下分解问题(递)
    ) Z3 p  |2 A' d* C4. **回溯过程**:组合子问题结果返回(归)4 p) v2 ]) D2 j+ K* H; T
    . ^( ?9 g! p, h* U/ D  Y! Z
    注意事项2 p; v, E( M# g7 h1 R
    必须要有终止条件9 l/ I) q# {, u# B
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)" Z5 Q1 m* N  N9 K$ u+ d
    某些问题用递归更直观(如树遍历),但效率可能不如迭代3 t" Y+ S/ N, m+ M
    尾递归优化可以提升效率(但Python不支持)9 o+ N5 }) A) O* [- M' v+ z

    . L4 A$ H! f: w- i, V9 X, E, C 递归 vs 迭代. |& M- k, n! p, X# E4 A0 F% R- t
    |          | 递归                          | 迭代               |
    ( P: X/ \- e9 t# R7 C0 T2 O+ j|----------|-----------------------------|------------------|
    ) p5 X- X( e! y% h. n4 P. m% T| 实现方式    | 函数自调用                        | 循环结构            |4 n; ^: O) S/ `% ^* a8 ^
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / ]) S7 S$ B0 V: T4 `| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; U# N& a, }* Y' e| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % u9 _+ z, D3 F. X: Y2 u* ?. U1 o" t2 g' {) Y
    经典递归应用场景
    5 n5 Z( a. X9 I0 g8 U% l. ?1. 文件系统遍历(目录树结构)
    ! M6 W$ X; t  i2. 快速排序/归并排序算法
    0 _5 T0 b$ S: i- [3. 汉诺塔问题
    7 J' N, p$ z: C4. 二叉树遍历(前序/中序/后序)
    3 b/ N/ m% v' _( l, `( E5. 生成所有可能的组合(回溯算法)
    " h% O0 ~% R! u; T' q! A+ `
    & ]: `  o" ?1 v- c+ D' C; T- x试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

    参与人数 3爱元 +26 收起 理由
    pcb + 4
    老票 + 16 涨姿势
    住在乡下 + 6 给力

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 14:35
  • 签到天数: 3132 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ k/ J* U' N' A+ V, e2 B
    我推理机的核心算法应该是二叉树遍历的变种。
    5 ]( {% I4 N) v- x另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    板凳
    发表于 2025-2-2 00:45:59 | 只看该作者
    Recursion in programming is a technique where a function calls itself in order to solve a problem. It is a powerful concept that allows you to break down complex problems into smaller, more manageable subproblems. Here's a detailed explanation:
    : f) c5 Q7 }% M5 S+ C6 `Key Idea of Recursion% Q+ E5 h1 |2 }& \, i* K

    6 ^- W# ^! q$ g3 v' b8 r3 rA recursive function solves a problem by:% x/ O8 @* t4 x& h7 l+ k2 @
    . g# Q& M& c% x" r, z- g
        Breaking the problem into smaller instances of the same problem.
    2 u/ `' ~) e4 M% C1 K) B( [+ g  L2 X7 S
        Solving the smallest instance directly (base case).
    . t1 n+ F; y. H/ [7 ~5 }& {; v; o: r9 p, r7 R
        Combining the results of smaller instances to solve the larger problem.  f5 b, z' i: w1 ]* [9 G
    * |/ w0 E# K' L7 W& k+ s. r
    Components of a Recursive Function
    " V1 Y+ \/ }, ~1 o3 w" w+ q% }& I9 w( x  a
        Base Case:7 T  K, ?& n: P
    & h2 h  E9 E- P4 s/ I- z
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 }, o+ d  e: m6 `+ V* J& P' o, N* `* V4 ]8 t! i6 f' d
            It acts as the stopping condition to prevent infinite recursion.7 l$ u$ H9 d  Y- ?2 A9 H; l

    1 ~- X, W) D/ v, N# x( r+ M4 a* w8 [        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.+ A* y% b4 V2 F) a0 a5 x
    : y# E/ T+ [% _
        Recursive Case:8 |8 i8 S3 D6 ]  L, e& l5 {9 a

    & Q% k3 V; [& e4 G5 S3 T; U        This is where the function calls itself with a smaller or simpler version of the problem.
    1 k) K. {! H3 ?+ I
    ' h2 ^3 L0 m; n4 c" j  G8 ^. ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! `5 c8 g$ _0 j$ h; ]: D

    ; ^2 V* [- H* r+ sExample: Factorial Calculation
    9 @! C" |% A- f2 ]8 L8 }, [0 z4 X: a1 y* \4 e2 W; d, O
    The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It can be defined recursively as:6 X1 A  T+ \& l0 M% I* r
    7 F3 c8 X5 [: n- P4 P' k
        Base case: 0! = 1
    8 K; B2 I# s: u  ?+ ]  j2 q, ^* E7 Z5 M: t! l
        Recursive case: n! = n * (n-1)!; P9 s; I- S: |
    / s. V# ~. _& _# H, w
    Here’s how it looks in code (Python):
    & ]5 e( ^' e2 Y+ C& bpython
    " ^2 C3 n4 V: K: y/ b; F2 I0 `3 T, j, V8 M4 i6 F+ O: z
    5 b* H* V6 U2 x0 z2 b
    def factorial(n):
    : P5 l" N+ J  f6 N) L    # Base case
    0 Z- U+ R% Y; |, ~7 ~! c    if n == 0:- T- z5 K$ u! A2 \* g
            return 12 c7 X- f; L! i
        # Recursive case1 i9 X& Z& v/ [' \( I0 D  l
        else:
    & U, |! ?9 l" h  A9 m  R. C3 R        return n * factorial(n - 1)
    6 j% B7 _9 k# D2 d1 c9 ^0 L4 I  ~# @: x" k' @3 u+ y' V: K
    # Example usage
    # ]7 Z+ y; ^9 dprint(factorial(5))  # Output: 120
    ' X) O- X" j# h# f' r$ r$ @& W$ T7 A* `5 m9 W/ ^0 z/ k2 v7 K; q3 H
    How Recursion Works0 x4 ?6 e/ s' t! f
    ( M+ O- N: S) K7 y% P; s' E9 r7 c
        The function keeps calling itself with smaller inputs until it reaches the base case.% T0 ]; P2 _1 w- {

    + x0 {0 a2 N  \    Once the base case is reached, the function starts returning values back up the call stack.# i0 J+ b) u5 d' s
    . R$ t* ~* b  x# T: F" f
        These returned values are combined to produce the final result.
    2 ]0 R& V5 t7 J( y3 {% y0 x: g
    3 Z+ E$ F6 Q0 n3 H+ {( W) |& _For factorial(5):" V6 f2 g$ n6 f! ?% i

    ' D4 J# l3 Y( J3 F1 d9 E! u8 u, \3 x
    factorial(5) = 5 * factorial(4)
    " j9 a6 h$ ?4 r! K; Z( C( f- }factorial(4) = 4 * factorial(3)% p, z+ k+ p- M* t; @1 f/ @
    factorial(3) = 3 * factorial(2)9 X& N  [! o" s' w
    factorial(2) = 2 * factorial(1)/ h/ k( [, Y! @5 s6 P# W) H
    factorial(1) = 1 * factorial(0); y/ j0 Q1 Z) `/ R' W
    factorial(0) = 1  # Base case* N( q9 h% E" z8 g) u" g

    6 P( h( P8 F( \5 C$ BThen, the results are combined:
    + i+ F3 T4 B  N4 |+ R' K$ H
    . [4 {1 U! i' Z0 e" B* Y
    2 v' y0 F6 _: x9 Sfactorial(1) = 1 * 1 = 1
    . n) @. k! M/ m4 [9 l1 ?9 }factorial(2) = 2 * 1 = 2
    ) J2 u( w) }. s2 e. {3 X  F- afactorial(3) = 3 * 2 = 6
    , G0 A* B0 B4 U8 L! Q. @factorial(4) = 4 * 6 = 241 K2 f% X: \$ R$ V- S# U* D' u" @- ?  ^
    factorial(5) = 5 * 24 = 120  N* a5 N7 ]2 t/ u% z

    3 f) H/ ~8 P+ ~7 u3 v9 qAdvantages of Recursion+ e& s5 R2 S' n+ ?
    5 ~) u1 l( |( l. T! i
        Simplicity: Recursive solutions are often more intuitive and easier to write for problems that have a natural recursive structure (e.g., tree traversals, divide-and-conquer algorithms).0 N% c, y& l( v
    ) I1 G6 @5 q- A  a" u
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    8 D9 q" t2 n/ g' u
    1 W) J9 B6 l2 A& }Disadvantages of Recursion
    ' ~- z, H' _) ^! i- w
    * }4 ?4 c. ?: O1 ^    Performance Overhead: Each recursive call adds a new layer to the call stack, which can lead to high memory usage and potential stack overflow for deep recursion.
    6 [8 g: q1 |$ R" j1 Q2 x( G! q
    ) c8 \/ B% |! ]4 ~. ]    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ u: V- O- K0 S- }, h) t

    $ G% m/ j' F: `When to Use Recursion5 H7 }1 L0 t; Z5 a
    2 I- C" ^" N, d/ j9 b
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    $ X+ ^, i6 y1 t2 u$ y4 j
    8 f+ C1 s# F9 F) ]# ]6 U    Problems with a clear base case and recursive case.
    7 j+ B5 c0 I) Q) m. C
    $ f- A7 K# B* cExample: Fibonacci Sequence5 ~. U: ]+ b2 V0 M3 k

    ' l4 x: `& p) ~/ T0 j2 pThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) e" h+ U# ~; }* ?
    3 J, J# q- G! Q* B8 \$ z
        Base case: fib(0) = 0, fib(1) = 1
    4 H- n# K% b" N# e; {: d" p
    0 |( o6 o* z7 L/ c5 z    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 Q( `. E6 z% G: q# v  \2 o: n% A; e! W- M& T4 m7 f0 C1 T
    python
    7 g6 a0 L) I) \' r8 ~+ k/ m: f% v' p
    % U% p% w0 G" e  j, R; H8 J) }. c
    def fibonacci(n):
    ( n; U$ t+ V" l+ w    # Base cases
    ! S$ ~$ M* C$ k: j    if n == 0:
    1 f- H3 U) H, s$ ], F4 K$ X        return 0
    5 c9 K" D. ?9 o0 l0 p, e5 }8 \$ z    elif n == 1:) }# l3 @# J3 d" Z" s7 d. E7 d2 o: S
            return 1& v9 r* Q" `! k+ o# _" N1 \& m8 P
        # Recursive case
    1 p7 J& P/ J) b& l, {) u1 H7 O    else:
    / B1 L/ d/ u6 S- j: i' D' z        return fibonacci(n - 1) + fibonacci(n - 2)
    ( I3 _* q+ U/ _" z& M; a! `$ ^8 |; [0 a9 Y
    # Example usage% K2 {6 [9 h  P6 Y
    print(fibonacci(6))  # Output: 8
    9 ]" V& z8 ]/ h8 e- a( J8 q9 H2 t! h/ V- o& K6 H& O6 m
    Tail Recursion
    / W$ Z( k' X' P, y5 L) J1 P# c) [3 Q# X7 i: O+ \
    Tail recursion is a special case of recursion where the recursive call is the last operation in the function. Some programming languages optimize tail-recursive functions to avoid stack overflow, but not all languages (e.g., Python does not optimize tail recursion).
    ' S; T0 G  i6 \; ^* w6 B8 W
    ( S" i: q# [3 r3 M# x3 zIn summary, recursion is a fundamental concept in programming that allows you to solve problems by breaking them into smaller, self-similar subproblems. It’s important to define a base case to avoid infinite recursion and to understand the trade-offs between recursion and iteration.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    地板
    发表于 2025-2-2 00:47:27 | 只看该作者
    我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2026-1-1 06:20 , Processed in 0.028876 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表