设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 d' W1 ?& ~5 z( k: j% c2 ?/ L9 n8 j6 e( u8 R
    解释的不错0 y7 R0 a; N1 f2 g1 }

    - Y; s& K5 n- g递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ ]- U* G1 h* b+ E" B& E  D1 U' w

    7 v: H) e# ~& z: J 关键要素
    7 `% R  A0 \  n) W0 ]9 ]1 e1. **基线条件(Base Case)**" P, u  ^3 k8 M0 N5 J' Y
       - 递归终止的条件,防止无限循环( `- {2 S5 v/ q! d+ Z3 T
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1. `1 z- n  l8 ~; H7 U

    ' ^  Z8 y! G" H6 }/ k& a; I; v2. **递归条件(Recursive Case)**0 I  U2 N1 I. V1 ~0 u
       - 将原问题分解为更小的子问题
    8 P6 c& N; i; ~0 c4 y   - 例如:n! = n × (n-1)!
    1 `' ?/ M! u- |3 ^
    , F  O9 f" T# ]3 Z$ O 经典示例:计算阶乘- F7 h6 U; Z5 q' l/ k( I) n( g1 ~7 E
    python
    3 f) ?1 d5 C+ Ydef factorial(n):5 v( E5 c. ^5 c8 j0 ]
        if n == 0:        # 基线条件
    6 n3 o6 u2 {& R) M4 z        return 19 e$ \+ E/ ]0 r3 }5 I: s! h
        else:             # 递归条件
    8 i5 d" o9 g" F+ V$ d3 [        return n * factorial(n-1)
    % q: r1 }8 e9 r% x* T3 A2 P# J执行过程(以计算 3! 为例):
    0 ^; p0 L# h( Yfactorial(3)
    ; S% V5 |- c" F; i5 P- H1 T3 * factorial(2)
    8 z& B4 e& ^5 L) @7 e& p3 * (2 * factorial(1))* |* g: }" ]% Z; [& K$ D
    3 * (2 * (1 * factorial(0))). A; b; N; w8 C8 @. h: M$ q
    3 * (2 * (1 * 1)) = 6
    3 X; v  a) t/ T: |8 r1 k& H+ A
    ' x9 ~" c8 S4 `$ d 递归思维要点6 C: Z6 r5 Q' u1 ^1 w' a) O2 y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑, y* X% b/ Z/ n  \" `7 z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)% `7 M6 N* R9 C- |6 P( R2 n
    3. **递推过程**:不断向下分解问题(递)
    2 a1 z) L& ]- I* a4. **回溯过程**:组合子问题结果返回(归)/ H. t* ]8 ~( z, ~9 y

    " o; l9 r* h# z( r" k/ V/ A; Z 注意事项
    9 t& O# o* s/ ]! ^2 _必须要有终止条件
    0 x0 `7 e. i7 ~; n( P7 v, P递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    3 [* p6 u( f4 m$ \某些问题用递归更直观(如树遍历),但效率可能不如迭代
      z" ^6 J. @  M" P$ W尾递归优化可以提升效率(但Python不支持)
    & \8 L5 y9 |1 ]* o9 t* A! d7 f: R& s& Y9 N0 d* u# k+ V4 O8 }
    递归 vs 迭代
    7 L5 Z- G3 [* F3 v5 `  D* P$ A+ D|          | 递归                          | 迭代               |
    , Z% h7 h. R" H* g|----------|-----------------------------|------------------|
    + g; `) ?( k1 w; P0 L| 实现方式    | 函数自调用                        | 循环结构            |
    , x, J2 O+ L8 z) p) p  P! s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |2 E5 o" u6 m( w8 N, A# V7 _9 _
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    - r- n# M. ]2 j* K2 @| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |- U- r6 ]+ e! R  }9 a  H0 M

    - G. Q8 Y( H; v1 {5 a0 x 经典递归应用场景
    8 o! G/ h# k. g$ @: a1 ~( j2 j- ^1. 文件系统遍历(目录树结构); o& f* }/ D7 @
    2. 快速排序/归并排序算法7 {8 g  L0 o6 K* [$ w3 b. z9 ~
    3. 汉诺塔问题& ^3 b- O6 O! k) P- v
    4. 二叉树遍历(前序/中序/后序)5 g+ Q, T. j/ m  d; W6 ^
    5. 生成所有可能的组合(回溯算法)- l. b" ?# e5 R9 c* T* M
    3 ^! T! T1 E5 o2 j/ y  B1 @
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 06:41
  • 签到天数: 3102 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ) g4 E' m/ k% i7 M" B2 X9 M: u我推理机的核心算法应该是二叉树遍历的变种。
    * p4 P# W* c( L5 M1 N; g* J另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    % j9 N5 n2 T$ D* l, uKey Idea of Recursion
    ( ]' q# Y8 ~9 `6 s
    0 r0 D8 V8 _+ u1 u. b- f! IA recursive function solves a problem by:% z2 S$ [7 Q6 T0 [* n

    ; H8 f4 ^: u0 O: t+ [) u1 }5 K9 U    Breaking the problem into smaller instances of the same problem.
    $ v/ B5 W+ Y% x6 u0 ^! O0 U0 G& ~) w' h. B$ m6 p
        Solving the smallest instance directly (base case)./ w" W6 e& Q' d  ]. A4 j
    + e0 O4 A, G1 w; y
        Combining the results of smaller instances to solve the larger problem.$ F% j$ d  {! j* V

    ! K+ i) `: }6 N# U9 oComponents of a Recursive Function* p2 b- s! y9 o( `) ^
    / B1 A, _9 j3 R1 N3 n
        Base Case:
    & y& D# }# r# z2 K
    9 U! h7 n5 D: Q- X        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: f6 z3 u# b0 ]. M

    # o+ s* |2 c; N6 ?        It acts as the stopping condition to prevent infinite recursion.
    # |& q8 t3 g! L; m/ C
    0 g6 U1 W( _% q+ U: R5 K6 O. |( _        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    $ Y# Q/ m/ Q4 t: ^8 |) N) m5 u9 ~0 O
    2 R- I+ t) K; u. S0 `) i  b; L    Recursive Case:/ s) |  R$ i0 Z; d4 h( G8 S

    * I0 r1 [' o, m! N        This is where the function calls itself with a smaller or simpler version of the problem.0 W& }3 l6 x0 O. l' O; l# j) n

    / Z/ B& l0 f0 s        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    9 J" @2 x2 a: C0 F5 }' h
    0 m" V* x) e9 w; ~* sExample: Factorial Calculation& ]1 T! x( W; ]' t3 P) j4 u
    + e  ^& R+ W  z: }) a, i
    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:
    1 I1 B0 }) x! i& i4 L- L# u9 L% x0 S
        Base case: 0! = 1" e+ Z# @" `+ g

    - ~! |1 p  ?" A" A3 q    Recursive case: n! = n * (n-1)!) s/ H' L$ F% H1 B- s" G" G- `; \, f

    ; m9 G. F' h! K8 I# `  AHere’s how it looks in code (Python):9 ?# s1 ], ^) f+ j4 \
    python
    0 r; t0 c  H" c: `
    # L3 ?3 x: @- D4 k- {  l  q5 G' |) i7 |& a0 Q  z' S
    def factorial(n):
    0 S' g0 s, V" _, B    # Base case7 V: @+ Q6 i& l5 ]( L
        if n == 0:
    $ r' r* ^5 l8 K/ S" b9 x        return 1, q, B- G) p0 S  y/ q
        # Recursive case! {" ^3 D+ d/ b8 W
        else:) j0 D3 C6 k0 Y; ~$ S9 j4 ~0 K
            return n * factorial(n - 1)
    3 ?; _8 U, a0 }# y9 M0 [6 Q# i& [: B6 q& C0 B
    # Example usage
    1 \0 j, S7 `2 T* Sprint(factorial(5))  # Output: 120
    8 v/ D, f3 U& o' q3 Y: U2 r* M% B1 b4 z! f
    How Recursion Works  S, x1 B& O  `! F9 [

    / S+ k7 J  |" N0 H    The function keeps calling itself with smaller inputs until it reaches the base case.
    5 @& |  b9 |/ b; h! G4 B
    3 E$ j# q$ L7 t/ L; o5 t    Once the base case is reached, the function starts returning values back up the call stack.
      F2 Q$ g, m: O  y" u8 S3 O  j7 C
    3 H8 e" K4 t8 U7 v" d    These returned values are combined to produce the final result.: O; B5 ?! r; j4 v8 T: j5 v
    : a  G2 V) n: d) Y, g' h6 U
    For factorial(5):
    " Y: O& B7 D. C& e6 a3 o+ D
    1 L; C: n6 n/ L1 @
    : V7 N( }# }: n, D2 Hfactorial(5) = 5 * factorial(4). I3 f% s6 ^/ k& b
    factorial(4) = 4 * factorial(3)4 d7 B6 ]$ V  R, i0 C! w
    factorial(3) = 3 * factorial(2). R& B4 t" v" n4 u( l, T1 _
    factorial(2) = 2 * factorial(1)
    , H( m! u+ S6 H* B9 {factorial(1) = 1 * factorial(0)( ], o% x$ t& R; a0 j
    factorial(0) = 1  # Base case# N# k$ z* t2 H: l7 A6 O2 Q( {

    * ^) a9 d# Q% ^+ _# nThen, the results are combined:
      \3 l7 [9 H% s* y+ }. A, A( r
    0 Z% C1 D( S9 {' A: j
    $ ^$ }) u9 k1 {4 gfactorial(1) = 1 * 1 = 1
    : g* v  w; H) |. q# T, H! mfactorial(2) = 2 * 1 = 25 Q( n. S6 u4 t6 [- Y+ n+ N8 S
    factorial(3) = 3 * 2 = 6
    / z2 F4 Y9 v& D0 V7 Xfactorial(4) = 4 * 6 = 24& p0 U8 U' G5 S6 |4 n" C5 l
    factorial(5) = 5 * 24 = 120, R2 N) q5 h7 @9 Z. O

    ! g8 R  d! e+ h; F, f1 uAdvantages of Recursion' ^& W* w" P4 e2 v  H9 g- Q
    & l1 [& M9 R6 m5 G
        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).
    ( r; r4 Q, J+ |6 J; b3 x8 |* i+ B* r8 y" i
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ K# x/ w* A% H$ p+ v+ \0 h" ]
    - D" c, q+ C8 L' i2 z
    Disadvantages of Recursion
    5 r! w+ i4 D2 s3 }1 ?7 v2 w! H& _  r4 J5 c! a! O2 R
        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.; B' Q! Z; C4 r, a4 X3 @" }- P; _
    # p0 w9 o$ k3 Y" j
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ) a2 L3 i; L9 h. z, ?
    2 r) I) B# V: lWhen to Use Recursion
    ; w+ O( k* I+ l% u1 X) L# u* z% M) H* I, R; |! G- T
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! O$ l2 V9 b5 i& z1 E

    $ A8 e0 [3 f1 V2 E    Problems with a clear base case and recursive case.) h+ Z$ U; ~9 r6 C& ]) |+ t, {( O5 g
    4 ^7 A8 ^+ V: Y8 Y
    Example: Fibonacci Sequence+ `7 m7 B' I' U% A& |

    ; x" m8 D7 `9 U' FThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# s$ W) s) ?9 P1 L  b

    # D; A9 V6 D0 q4 H    Base case: fib(0) = 0, fib(1) = 14 V6 E$ @6 p7 e- Z$ y9 g* h
      x) v' a5 g: _
        Recursive case: fib(n) = fib(n-1) + fib(n-2): w1 z& J3 x* j' q
    * }8 P3 b2 g) b# v* `4 z% ]% T
    python
    5 I: h$ o3 e& e  ~2 x- x4 l* X( T! N: b- E, L, D
    ) o/ G, c. X" Q. g/ A
    def fibonacci(n):4 A  [9 Z( q  i+ X( x3 H+ k/ j
        # Base cases
    . \9 p1 N/ C: U$ i3 Z0 O    if n == 0:2 U0 z9 g3 Q7 {$ S3 \2 E1 i4 }! W
            return 0
    3 x3 `- J" n8 g" [. y    elif n == 1:
    ' y9 @4 ]0 M% _% R/ I% m        return 1; P4 U3 {; `  r+ \* G' w6 S
        # Recursive case
    ) m7 u0 ?2 D' D    else:
    0 P+ g! D, y& s) L        return fibonacci(n - 1) + fibonacci(n - 2)
    * d6 r/ n3 O3 O1 p9 s9 r
    " X( Q/ z) m6 P( F0 F# Example usage. M9 L5 u2 W/ v8 ~$ j& }* F
    print(fibonacci(6))  # Output: 8
    . I, y. R1 C' f+ B" ^! ?7 S
      ?8 o- z& q1 I4 k9 d2 e/ h( HTail Recursion
    $ H! p9 j  u4 T/ C& d
    : O+ T, I; E: O3 H6 r5 X2 A+ }3 nTail 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).
    - X7 |3 s% _3 ^& d# [8 e" |* w% ^! N$ z4 c- G
    In 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, 2025-11-27 18:27 , Processed in 0.032751 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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