设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    , ?8 g% H  |+ Q7 @+ S7 X( w
    + ^# {9 T# f* Q1 i4 e+ J2 r, i; d解释的不错
    . B$ ^# O! ~+ m; q1 }0 j
    7 [' P# Z3 q0 N4 ]递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。* b& E5 k# Z* }9 p1 T, E( i

    / l; x6 @8 D, \ 关键要素/ ^4 m1 A/ i2 O8 h# @$ U8 s
    1. **基线条件(Base Case)**& b0 U) h* C: s8 Z5 {. |8 `
       - 递归终止的条件,防止无限循环
    : p/ M+ D; b6 {' F   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    - A/ {4 P% K; R% x
    ( W, O  R1 B, C" @/ z) b; ]2. **递归条件(Recursive Case)**+ h1 s4 Y  B5 ^5 ]. ^* X8 D8 r
       - 将原问题分解为更小的子问题
    & D( D+ ]. h4 H0 v8 n3 O+ d   - 例如:n! = n × (n-1)!& @5 r! n8 J: Z1 s+ Z! W, T
    5 d4 {/ u5 m3 m$ k3 u5 d( x
    经典示例:计算阶乘
    0 L$ n1 P; o5 a* j5 v$ q5 ]python4 V' x( ^, B3 t4 l3 a6 f7 I9 A) k0 d
    def factorial(n):3 K- o3 V; B- L! H0 o& d
        if n == 0:        # 基线条件
    ( M) L. d3 O! R4 q) H2 m  b  ]" P! {        return 1% b# ~/ l5 [2 h; H, ]1 H# F
        else:             # 递归条件
    " U- f# Y, O3 P# Y; m2 L, M( ?        return n * factorial(n-1)
    6 a  N1 x9 R' J执行过程(以计算 3! 为例):
    1 }& s9 z3 a* _% q- c) O' Afactorial(3)
    ; e! i' v& T+ z1 Q% H6 f: `3 * factorial(2)
    ( |. h! F0 j; v3 X+ G9 H$ R4 u3 * (2 * factorial(1))
    ) w/ a3 E1 ~: C7 o* [) c3 * (2 * (1 * factorial(0))); e& c! ?: B' O3 t0 ?
    3 * (2 * (1 * 1)) = 6
    $ I& F4 q8 G9 M: L% A1 O. U( Q! q: }
    8 a- V# M( K2 r3 W7 }+ w& I2 u 递归思维要点8 F( A0 d) y7 `
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑9 f2 d4 T/ G& Y; \  E& j) J  [
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 f9 c5 ], @$ I+ e' B3. **递推过程**:不断向下分解问题(递)
    0 y! H5 |8 Q* R" U, h+ Q3 \4. **回溯过程**:组合子问题结果返回(归)
    3 @+ R# \1 B  @# \, K6 R$ c7 C0 F0 A% m; L1 r5 C
    注意事项
    & Y. i5 Q. c( w, T/ I必须要有终止条件
    8 l! l9 G+ r1 }  \5 p6 @% B9 p递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 L1 K8 V" X/ _3 s8 g
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    / o+ n& w! ]1 C0 Y5 @" g6 A; J尾递归优化可以提升效率(但Python不支持)
    ) Y  `. i; Q( a/ t, }5 {+ o! c+ t
    递归 vs 迭代0 W% U3 j. s) B( K0 b* ~7 H  p0 R
    |          | 递归                          | 迭代               |
    0 K4 s) k" E1 @% P( \) d|----------|-----------------------------|------------------|0 o) M. U; M( E9 c! d& T7 N6 F
    | 实现方式    | 函数自调用                        | 循环结构            |9 S& P0 t% L0 d9 Q$ _
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |& g" o: x' _7 t' Z1 @- V7 V# f
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    9 Q8 [  f' Z- |& G2 z, ]" A| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |* J4 a9 ?0 D9 t* E
    2 T: M3 i) H& q! [1 ?* S; l
    经典递归应用场景
    1 s8 T# ]$ f0 F9 r1 F1. 文件系统遍历(目录树结构)2 J9 i' n  V$ J2 D
    2. 快速排序/归并排序算法
    / D2 ~2 v  g  \3 }! Q$ b3. 汉诺塔问题
    : _5 c8 J3 @: g, n) a4. 二叉树遍历(前序/中序/后序)+ G! a% o  G' ^# I% C+ O. k( Q0 l8 W( E
    5. 生成所有可能的组合(回溯算法)
    * O6 m( ?9 {* Z: ~: }! ?& L: j" |! u2 L" l6 y7 i# q
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    10 小时前
  • 签到天数: 3144 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 `; Q8 x, @, X0 ^. a
    我推理机的核心算法应该是二叉树遍历的变种。
    2 E# O# y2 O8 P& A2 z# j: k另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 P$ x2 g, p, l$ u. dKey Idea of Recursion+ M. ]* P$ G. l9 ~6 H) U8 G

    - g' K# W6 J$ K4 Y& z0 }" lA recursive function solves a problem by:" J$ r+ g+ R6 ]

    / H& n& ~2 ~9 J1 G) p; b    Breaking the problem into smaller instances of the same problem.
    - _- d" ^4 O% t2 W, L3 ?: W5 c& Z  {; O8 R1 \8 S1 y# l
        Solving the smallest instance directly (base case).' q6 Z2 |) w6 M# ~& S3 W' L
    0 {* m; |/ V+ f9 e9 |) C- D) ~  h
        Combining the results of smaller instances to solve the larger problem.8 D& Y: q4 r% K* j& l, Z

    ( B+ G  {' ?! uComponents of a Recursive Function7 O; G4 i$ D5 F1 d2 n/ p- w

    6 i' [, d/ L8 D! Z' U/ t$ s    Base Case:
    & a3 Y+ C& e( K2 ~& |
      @) g* L: y' M7 c) w' R+ b, F  u        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 ?8 V* v, F. h5 O: [; t
    $ a& _# V/ v- O, H5 F+ p7 ~        It acts as the stopping condition to prevent infinite recursion., I" i* J- O7 \

    ) J% h' d/ D% s        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.6 C7 `1 `/ e0 ^3 Y- ^) D- s

    4 ^; b  N  v# O3 v    Recursive Case:9 p- _1 P% O  }  ^5 k

    8 h6 }+ g& Z; ^4 E2 O: w        This is where the function calls itself with a smaller or simpler version of the problem.& U# d+ K+ s) }6 H' B' r1 c
    + e9 K+ z+ T6 s; x
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    1 w' Q6 R- u$ d$ q$ C) h) Q
    5 [6 E) T: y+ I2 `$ p* N5 R$ ~5 ~Example: Factorial Calculation, k/ [6 ?% M1 v  w) d- V$ k

    % O; e( h+ g/ I" UThe 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:
    $ E; `/ @7 a  b" e8 p
    : k, l0 y- W9 \6 N* W' H7 a: f    Base case: 0! = 1
    6 [! ?8 G7 Z- v, o' W5 d4 I; Q* A' g
        Recursive case: n! = n * (n-1)!+ E& C. S4 X8 ~6 r# p
    0 z/ @( w, B9 P6 w6 y& w
    Here’s how it looks in code (Python):
    % V8 `) k. W! t9 \python, v5 K  i! |2 i* {4 {, l
    - |4 [& I+ k& {
    - l' k( \9 w' U' K6 e; b/ y( ]
    def factorial(n):
    4 l1 c7 O% @% a2 h4 ~  d& Y3 _1 \4 ~    # Base case
    ! e$ H4 @5 ^9 U( L; C  u0 v    if n == 0:: m1 |" ?; U& O5 ~% H
            return 1) I  k* n' p! \$ j
        # Recursive case& Q9 ^% _+ ^! I) k9 s3 l5 B" y$ B
        else:2 P  t3 k/ c( o
            return n * factorial(n - 1)$ ~+ ^3 C# S, D, J0 V5 l5 F

    * |8 F" H4 B9 {1 s0 d) y2 D) }# Example usage7 P" m) O5 S3 ?9 |0 u+ ^" w
    print(factorial(5))  # Output: 120
    & K" V* d) M7 |. f8 i( v9 s2 H8 k7 s0 f2 B7 f% \
    How Recursion Works3 W7 }7 Z1 }# I
    ; S3 z8 z1 M3 k7 {1 x8 W5 x
        The function keeps calling itself with smaller inputs until it reaches the base case.
    8 h0 \9 |2 B; [! D7 q/ h  ~( q0 w5 g7 @. f
        Once the base case is reached, the function starts returning values back up the call stack./ g& I" J4 I  X5 U8 J
    . Y' B7 @, X, [6 A
        These returned values are combined to produce the final result.
    # B/ R& c7 m9 z0 Z0 d" ]) \1 S7 `3 S4 l5 ^* ^; _/ c
    For factorial(5):/ O8 V" v. S! g' o5 l  h: u
    . Y: q' T' x$ n) c

    0 Y1 u3 x1 N* bfactorial(5) = 5 * factorial(4)
    8 Q' p. {6 F- n8 \$ V# Gfactorial(4) = 4 * factorial(3)7 A& h5 \" ?4 E  X% a0 l
    factorial(3) = 3 * factorial(2)
    . u3 k; [- z* E" ^factorial(2) = 2 * factorial(1)
    . _* w- F. C; W# M: q( Z( I" |* efactorial(1) = 1 * factorial(0)
    , u8 `/ u! T( L1 O% l8 bfactorial(0) = 1  # Base case
    1 I4 U( H3 X" ?1 ]& p' p$ t+ G% t. e5 ?. s0 z2 L; F6 E
    Then, the results are combined:1 v. \$ z/ ]1 D8 C

    ) E! j& ?" A( t  }8 F- e, p! q- K4 h
    & |3 W  U/ n6 }7 d( v: ]2 [factorial(1) = 1 * 1 = 1# Y5 E" \$ K' L' @2 H
    factorial(2) = 2 * 1 = 2& |# V& Q3 o# _
    factorial(3) = 3 * 2 = 6
    ! Y1 G9 A6 `) y% K: qfactorial(4) = 4 * 6 = 24
    7 Z" n) {. }& w) s3 G: r" Vfactorial(5) = 5 * 24 = 120; S# R$ {0 @3 L
    " e8 C0 P# V, }$ C5 d" c
    Advantages of Recursion3 [5 `  m* }& y
    * p5 E) U! `# _4 M
        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).
    / F# V$ A1 }9 Q5 s* P9 T$ a$ V0 e' D6 K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ Y0 B- x8 R# O5 b; L
    9 x, a! F5 c! I! D& y  S% |
    Disadvantages of Recursion- e3 G3 j% N' K
    1 m7 f, x1 U9 l. p  ^: `6 f* Q
        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 I% [) v) P2 x5 L* Q

    & S) O* {8 z9 _) f$ v- R    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & A8 L# E3 s6 ^, p' P: ]' W* ?+ }& f2 Q5 o7 m" V) N7 ^, S3 O+ ^7 ^- |
    When to Use Recursion! _( ~( b+ N7 q3 |7 ?
    $ \3 K, f) v3 u0 {% h" _
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ! k" v- U1 Y0 @' ^# y1 Y+ _6 I: d/ }4 V
        Problems with a clear base case and recursive case." Z8 R2 X' z; _4 o- s* j. }

      l/ H; U  Q( R* n& s2 E% Q7 wExample: Fibonacci Sequence
    / K+ ~' ]7 K2 t, j4 Y( S% V
    " T5 Q) [1 i  k+ tThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    1 r9 B1 k+ w& g* j( _7 t+ B$ K3 ^: i
    $ f% Y6 J3 B2 O1 T& S/ h  X, {3 N    Base case: fib(0) = 0, fib(1) = 1
    ! |) a9 F& P+ [; e: H  J
    6 c7 v2 O) L' c+ [7 X% W2 u! I    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' r: t, q: [5 ?3 a& x; d. ]( U- n7 V/ V6 f7 U
    python0 |0 v3 S4 s# s, N% O+ {; ]
    : t( G6 u$ @% [6 n9 d9 k
    / x2 K8 l+ M" A. c
    def fibonacci(n):
    & M  K" y5 C1 h( g2 J, U    # Base cases4 i: h/ {1 q/ t! n. ]
        if n == 0:2 R) u( X  w: n( N4 d# d: a) j
            return 0
    0 R! G5 ]4 H$ G8 L    elif n == 1:
    1 O. X" q1 r4 E% B. G        return 14 z' t! n4 |7 U' H$ N
        # Recursive case
      a# t- Z! a( j; w* v0 V/ f    else:
    ; G3 n( A* ]. }* s        return fibonacci(n - 1) + fibonacci(n - 2)  r4 x" B4 V1 `0 C4 h1 @
    0 i4 r# @% p  W/ U6 ]1 f/ u
    # Example usage
    7 m: ^, S  ?/ |4 Dprint(fibonacci(6))  # Output: 8+ T9 h8 M( C5 ]! Q3 u& ?' j

    " f2 d; g* B& q) o+ ^+ g. R7 g# rTail Recursion7 O" M- c+ e6 Q1 |5 v2 v5 @
    ! Y' R4 ^( \: d
    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. v& L5 t3 s( m8 ?1 U- D6 c
    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, 2026-1-12 17:57 , Processed in 0.029666 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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