设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    + \4 s) Y6 A5 P) ]
    # r8 Q# S, u" l; T8 s0 Y解释的不错
    # J& p! [- W* r  a* j$ f1 O6 w$ `0 B# N
    4 n9 v3 `( B- ~递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。# G5 `+ F3 D5 m' L% s2 l8 W5 c

    & l- i9 A$ F) |2 a 关键要素( b+ Y, E3 J3 w" f, \4 [
    1. **基线条件(Base Case)**
    % G; n* R$ @0 j' q2 @9 H' J" e   - 递归终止的条件,防止无限循环
    / ]+ H; W4 N& ~+ F' n0 i$ `   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ! X+ H" ?4 f* U
    : G1 V4 j4 B+ C$ e2. **递归条件(Recursive Case)**
    7 `3 [! H, e! A& ], k   - 将原问题分解为更小的子问题; Z8 B$ b% @' I% e4 N
       - 例如:n! = n × (n-1)!% ^4 D0 W% L" ^9 |7 X6 O. A+ K

    . P9 y( u% E+ n0 }: ?0 x 经典示例:计算阶乘5 A$ A$ E5 L* f0 c7 u) L+ U
    python. Y) A0 D, K0 B1 o9 C2 F) v$ G
    def factorial(n):
    " W# K0 A5 k8 I7 o- z/ j    if n == 0:        # 基线条件2 t' [, T& L6 |2 h& Q& Z
            return 1
    - B1 ?& i6 Y' F2 Y    else:             # 递归条件6 v- K- e- ^9 c
            return n * factorial(n-1)
    4 q' [& s* _8 ]8 q执行过程(以计算 3! 为例):
    0 C) f$ h& i1 p1 Efactorial(3)/ o- P5 U0 s8 S: H
    3 * factorial(2)
    4 H/ A( t+ ]/ C8 Y3 * (2 * factorial(1))
    6 v: w8 A$ s* X* `% Z3 * (2 * (1 * factorial(0)))
    5 [6 [( s1 T% I" M0 n7 \3 * (2 * (1 * 1)) = 6
    8 I& F# {& R$ X/ Z! u% V
    ) c' b4 S0 H( x) u" f 递归思维要点  l7 {4 }2 f; {5 d# ]6 L, {
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑/ T7 I) L1 ~, U" Q! Y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ' f5 f8 q, X# M. U0 X- X3. **递推过程**:不断向下分解问题(递)
    7 h% ]* @, H2 f4. **回溯过程**:组合子问题结果返回(归)
    % y/ X+ i: ?$ D3 q
    7 r/ l) |. b" t6 v 注意事项
    % G- p7 r( r3 u6 `5 ^必须要有终止条件
    ! ~( K& I  ~, O' I$ v# {递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    4 M) z$ \3 [" q- S, L某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , I, C  N  Q5 }7 w尾递归优化可以提升效率(但Python不支持)4 B+ ^" e; a5 n$ S8 r! e/ l

    " ?( l0 o/ }2 p% R5 P7 L1 q" k 递归 vs 迭代
    9 j- V- J$ p% f; M6 @( C8 h: G7 y|          | 递归                          | 迭代               |6 j9 d) J% T/ B  ~+ a+ [
    |----------|-----------------------------|------------------|/ P: K4 Y" ~; u5 A& U4 x
    | 实现方式    | 函数自调用                        | 循环结构            |
    5 j$ [9 J0 p! a8 S. D& `6 B3 {: [| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |0 y3 j4 Q6 F) Y4 h0 F/ N' M
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 `4 L) _3 I+ |$ V| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 `7 f$ ?3 J8 ^& L8 h6 N
    : _0 r$ ^  f. O  t- Q! Z" H4 L 经典递归应用场景
    . I- P3 T8 x9 R8 |/ C! ~1. 文件系统遍历(目录树结构)
    0 K3 P, L% a" i# F; P. M2. 快速排序/归并排序算法) C/ U  m9 h4 u% w9 H, D1 f  k
    3. 汉诺塔问题
    : C* ~0 s7 p9 Z. |# v# K4. 二叉树遍历(前序/中序/后序)
    ) s9 n5 K2 d5 x7 ]5. 生成所有可能的组合(回溯算法)9 V2 @  V3 h( Z$ z: x
    ; W& c6 F) t" N( C% o# ?
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    2 小时前
  • 签到天数: 3193 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 b! s5 H% F+ R% F, N) K1 w  m
    我推理机的核心算法应该是二叉树遍历的变种。
    " H8 I1 r, N  t) N2 Q8 f另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:2 V5 L8 E0 y: L9 T
    Key Idea of Recursion
    2 r" j7 t/ g; j* W$ j; m6 Q% U! v1 {- K! U* B5 ~' ?- D
    A recursive function solves a problem by:4 h' v7 y$ d7 L5 j6 b- q
    : ]" k" Y4 ~4 `2 f; U
        Breaking the problem into smaller instances of the same problem.5 H+ X0 s( J( o  F, A  m

    / j% T- t$ n& v; x8 A    Solving the smallest instance directly (base case).
    7 r# g- G$ |5 W4 [6 V* n- a, J0 K# f8 p1 \; H
        Combining the results of smaller instances to solve the larger problem.
    ; C  F2 p. L5 k# y& Y5 i. o" e5 \
    ) |1 ?* m8 H  f  b. yComponents of a Recursive Function8 R. u, P( U6 ^* G" a
    9 l0 w+ m. k/ ^6 v
        Base Case:
    5 s& U7 Q) M  x, m) Y! Y: I1 F) \/ y$ m$ K' l6 i7 L- ?
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ; \. e2 c9 |4 ?0 [2 L1 N- @$ _+ f0 D. T- g
            It acts as the stopping condition to prevent infinite recursion.
    " W8 o2 F# T- P0 K/ X  G
    ) @% A* l- d( {        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.6 b+ I( m3 n( M' t9 y) W
    - ^+ m* K* y1 M/ E: ]
        Recursive Case:8 H  p5 L" I* }+ ]5 ?  u* b) z9 u0 w
    8 _! \$ a0 p" m. p' u9 ~$ \
            This is where the function calls itself with a smaller or simpler version of the problem.
    ; {- s* L. {/ y# i
    ( s9 Q% z6 N, @$ G7 a$ K5 o0 o' o        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' C4 i+ s2 q& ^. K$ U: O8 X0 Y1 k
    ; n) g* h# O# H7 n9 q2 G
    Example: Factorial Calculation6 @1 t7 ~/ f: r) ?9 S

    2 o0 {2 Y) M& P4 q& DThe 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:
    ( j! I1 X" Y8 L* L% C% w3 }2 n! ]; }3 D, g( C: w1 E
        Base case: 0! = 1
    4 E- j$ z2 e' k" W9 z- V4 F7 G7 {! Q# \% l) U
        Recursive case: n! = n * (n-1)!; ^* H6 p/ f: O- J' ?$ x3 ?+ f9 d

    1 O; Q5 b, P. P' p/ u1 P. `/ z: C2 hHere’s how it looks in code (Python):
    6 x' e7 f) Z. J9 M, lpython
    & r: d, T  z9 b( ~) Y
    7 G+ t9 }, V' L) u+ q5 F  V' R3 H- G, \; t
    def factorial(n):/ K* |& j" L9 E) d; i2 M
        # Base case; A; `; {8 d& [0 R+ o. {) F2 B
        if n == 0:
    1 B/ F" j8 m& @0 c        return 17 D( K- t# p+ ?
        # Recursive case
    8 P- _2 ~+ X2 U4 a6 g) E1 \    else:2 U0 w, W2 T' t% I2 s  T( q
            return n * factorial(n - 1)
    3 r% ~2 C/ @# P- c8 ?& P* W, }; T. @( `. q+ u
    # Example usage
    5 w( e8 r3 p# V+ K" b9 L; fprint(factorial(5))  # Output: 120
    ; j8 B: u' K) g# L0 \' F3 s
    / X8 I5 {, M+ H) UHow Recursion Works7 Z& W9 r1 R4 k8 j

    " H5 m5 H: T! G4 e! i    The function keeps calling itself with smaller inputs until it reaches the base case.
    . |8 t. ^* s! M0 P$ q1 L6 c8 E! R3 c0 k9 o- B
        Once the base case is reached, the function starts returning values back up the call stack." j9 {) `5 V" U" y: B& i9 D

    & |- r0 w# X% z% u1 X5 K* m8 k+ C    These returned values are combined to produce the final result.0 X+ m$ L; T+ o) `! t/ ?

    9 I& N" U: z% n. t7 U' B! V: T) `For factorial(5):
    + [' I+ o& x7 ^  a8 U
    - Q" f) e7 Q5 }0 m5 O6 G" R$ t2 Y& M1 K; u/ V
    factorial(5) = 5 * factorial(4)
    1 Y4 w# \9 T  w& T/ i3 ?0 ~factorial(4) = 4 * factorial(3)! P0 t' A3 ]  o* ]; O
    factorial(3) = 3 * factorial(2)' A! u$ W4 S/ |
    factorial(2) = 2 * factorial(1)/ R# u) i& f3 t+ O/ O: o' m! {3 J
    factorial(1) = 1 * factorial(0)4 E8 m  w" ?3 j& J9 j2 Q/ {
    factorial(0) = 1  # Base case
    4 b  Y. q# I% i. Y+ `) O$ S
    7 h! r- N' ^0 H+ y8 t6 G9 _Then, the results are combined:2 U' C5 x* o9 a/ C+ D0 g" r
    ; A3 R( S) l$ a, P; _  X: }

    - |: x( K' n' J# E0 v  Wfactorial(1) = 1 * 1 = 1* C9 _( _9 j) O2 U8 D& o6 f( j; p
    factorial(2) = 2 * 1 = 2
    $ a: }1 N/ P9 n: Mfactorial(3) = 3 * 2 = 69 c0 A4 ?/ e4 y" v7 D/ Y$ l
    factorial(4) = 4 * 6 = 24
    9 ?7 g( y& O+ Q" @4 D- I& r/ u$ M; Afactorial(5) = 5 * 24 = 120( e; s  O/ [9 ~- S6 _! ~$ `

    + m" c3 c0 {$ R; jAdvantages of Recursion
    ( u4 L( ]# I: t$ p! A
    5 G! y3 z( t# N9 v; m6 G7 h    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 b- |3 i1 m7 ^/ x1 N- a8 b2 @. ]; H, r0 H) Z* j  }" S
        Readability: Recursive code can be more readable and concise compared to iterative solutions.8 j; B: F; s, B% n
    . S( {7 \, d4 `8 r/ b* r
    Disadvantages of Recursion
    ! c+ |( B7 X1 s9 a( u- Y7 \" v
    # `" t! z2 A5 v    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.
    1 h. h, e8 V& L# X" p: P4 L; H  s
    0 V; w2 n) @+ A/ S! H    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)., Q# _5 y1 W9 O6 x8 E

    0 }2 \$ x+ }8 n( u' }  E6 FWhen to Use Recursion
    0 V; h* r0 a2 U) A3 ]3 A& N& O, K, w9 h6 u8 j0 J' H1 ^7 a+ t- M9 G  S
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# c! I1 p  d  c, E* r% I6 o& c' A
    : E4 q$ [* Y4 q0 r& U
        Problems with a clear base case and recursive case.# O2 G5 ~, d' w5 N  }1 ]) f
    2 w! @0 E" J; \) p" {2 k+ j
    Example: Fibonacci Sequence
    1 D' [$ S, F. T+ Y! ]% e: K- p
    ) g* i3 X- ]* T. u, o* ~The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - s3 L" T0 r/ o8 F. }" l8 x$ \
    / |' I1 y- X) ?" h    Base case: fib(0) = 0, fib(1) = 1
      x$ t  W; @2 n8 H9 y# X
    . T% ?* R" D$ }! a8 t    Recursive case: fib(n) = fib(n-1) + fib(n-2)6 G1 X3 M- M: J7 W
    * B" z' s0 e( ~8 l- g
    python6 e6 i5 P( c1 q& \; f; o. v' W
      k& r# R" l0 ^- I4 y

    0 k$ e  r, ^9 U8 I+ Hdef fibonacci(n):
    0 m2 s* @7 y2 }7 t7 Q! K    # Base cases" g6 r5 ^% h9 a/ O/ P6 b2 X% V
        if n == 0:
    ! B7 \0 d: C. }% B        return 0
    ( `5 P& h6 L7 v( Y8 I5 b* P3 O, N    elif n == 1:
    0 X* H, E3 n# A; w7 _% _        return 1# P1 E! U. g* A, _7 G5 N, O
        # Recursive case
    ) H) g7 l4 s7 c6 Z+ c' G$ r    else:& |; h& d8 O0 s! W! }) L
            return fibonacci(n - 1) + fibonacci(n - 2)
    ' h0 p- z; f4 U9 c1 I+ h/ R7 m0 ]$ ~3 v
    # Example usage* H  S8 _" c' r: g7 m% k& {
    print(fibonacci(6))  # Output: 81 x9 A* h3 l+ [5 I

      l  C3 H2 J6 V' t$ VTail Recursion
    * ?) c( Y& J1 Q9 M' [( J7 s4 n2 N  ]- U) D! a4 C8 Q
    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).
    0 q& A1 p+ S( C0 V. @- s
      }+ z* P5 h1 u0 qIn 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-3-6 09:41 , Processed in 0.060200 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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