设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 # j# N6 `5 m, s; v. E

    3 @! f( }# A6 X8 V$ D解释的不错
    / i7 Y* h( M" p5 ^
      j( L7 D4 O& T* W递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ ]2 {& {: m( |# `# e1 C4 u$ L7 F* `$ g% r6 Y' G9 m" ^' ?& b7 `. u2 X
    关键要素6 G  Q. o/ M/ U% P! e# X. h! b
    1. **基线条件(Base Case)**
    7 n+ N: R) T4 \( @! E: _# Q- ?0 U   - 递归终止的条件,防止无限循环5 [( J8 d2 n- O" L/ s  o* ~2 N
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1$ p' [) q9 N7 J0 `

    6 V2 E3 k8 x+ u2. **递归条件(Recursive Case)**
    8 j! K; G8 Y2 h+ F) x   - 将原问题分解为更小的子问题
    0 T, S; K- ?+ J8 o. w5 i6 M2 a   - 例如:n! = n × (n-1)!  H1 E8 ^8 r  x3 a( y: t8 u; m
    $ \% R* j; M# {) K3 E+ b7 t' J0 T
    经典示例:计算阶乘
    - R# B, ^* Z0 {2 j) T! ^* _python" U) k6 l$ i! v
    def factorial(n):
    $ t  I3 `3 [& |' ~6 @; |0 D7 t7 q    if n == 0:        # 基线条件
    ; A# u2 ]. M9 T& v% L        return 1
    % Y, m" o  b7 w% }2 Y2 B6 W    else:             # 递归条件5 x  v! l* Z" B. X* k
            return n * factorial(n-1)
    7 ^. ^: @' C* g3 U/ K5 u执行过程(以计算 3! 为例):
    0 @- |* @  w4 U/ y. ?4 w* O& R2 Wfactorial(3)
    ( Z9 a& C1 Q0 x6 X" v3 [3 * factorial(2)
    ! ]- c4 G- L+ [, _* R+ R3 * (2 * factorial(1))
    4 `2 U- Q# H1 Q* w$ `3 * (2 * (1 * factorial(0)))! a1 ]! B  \/ T: I  V* o  |' a% x
    3 * (2 * (1 * 1)) = 6
    2 Z1 B; F3 ]  _+ N- o3 O: S8 [. l9 x
    递归思维要点
    ( d) x3 N: n9 w4 y1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ; V/ N; F, D) ]+ r7 f% j! @2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 j. v3 ~+ ?7 ^/ U3. **递推过程**:不断向下分解问题(递)7 b( t: u9 e# ^3 }0 ]( G! Z) f. [
    4. **回溯过程**:组合子问题结果返回(归)5 K& D0 _) k: E; `
    ! \9 g' `( f6 J
    注意事项" X  X* Z' R8 x( Z7 @0 k
    必须要有终止条件
    * L& l: ^/ l6 b6 N% I0 R- Y递归深度过大可能导致栈溢出(Python默认递归深度约1000层)# \0 t0 @5 C1 ~7 `
    某些问题用递归更直观(如树遍历),但效率可能不如迭代# M+ U. u8 |  k  L. A
    尾递归优化可以提升效率(但Python不支持)
    ' t# X; |" l* [; J: D
    3 y: V9 n/ V: r. r% `" M 递归 vs 迭代
    6 e* P8 [( V$ @- o|          | 递归                          | 迭代               |/ `3 ~, u5 T* W
    |----------|-----------------------------|------------------|
    ! v: S2 J  R4 M: W| 实现方式    | 函数自调用                        | 循环结构            |
    , I6 a7 x0 N) @: y9 p| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) a1 z8 G3 S9 |1 g+ U' E7 ~7 j
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    0 r" d' x! D$ c; z  o| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% l, Z& j; U! V' O: F1 R

    3 P7 g2 k& U1 w  P/ T 经典递归应用场景& K; q+ J' }# U0 M
    1. 文件系统遍历(目录树结构)
    ) w4 w& h1 R! \" M3 h6 p+ I- Y2. 快速排序/归并排序算法
    2 @6 V$ L4 \5 p3 u1 P. @7 {3. 汉诺塔问题) Q% j$ m3 `6 k1 \+ W& l# i
    4. 二叉树遍历(前序/中序/后序)
    / Y6 r( ]6 C# L. K  S0 s5. 生成所有可能的组合(回溯算法)
    5 ~, A: |1 G/ ~& w" f
    / ?0 C# u) P7 E7 Z试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 09:55
  • 签到天数: 3196 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,; v7 w4 Y" c2 k1 h" b
    我推理机的核心算法应该是二叉树遍历的变种。
    : w; K, G" a4 j# 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:9 e5 V5 H8 _/ e  l* e; q
    Key Idea of Recursion
    . a8 n( ^7 I# G/ ]& `
    6 I+ \( I4 ~9 c3 v1 Q4 @A recursive function solves a problem by:
    - g8 u# e1 y- k2 \# ^6 ^- X2 b$ a" G$ C1 S  j; b2 j; }
        Breaking the problem into smaller instances of the same problem.
    . m8 I. N% @0 \+ Z3 i0 r! v; f# v8 d, ?  B
        Solving the smallest instance directly (base case).
    7 K0 A7 ?0 [7 `3 W  q9 Y& x4 C- R8 A' s$ Q. I! u& @
        Combining the results of smaller instances to solve the larger problem.
    1 D9 q: I9 M- o* M' @4 D9 G, s) z2 D0 h8 l/ E! C. I+ j: I
    Components of a Recursive Function
    * @( F. Z, d+ W/ \1 }0 X/ c. ?+ f; E9 f& a+ P, q9 @
        Base Case:# H0 f: B6 h+ o# r! D; m
    - ~5 o: ?! P; H
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.( P) m; x( O1 ~8 U  _4 o

    ; s& B- Z. ~) j4 `* T; Z        It acts as the stopping condition to prevent infinite recursion.$ S9 T4 _0 Z" _& T9 M

    * W0 d; Y& T* q: Y1 ]        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 c9 L3 H& I0 ?, `* Z

      G% |5 l% O7 M6 V) {. W    Recursive Case:; g) E! t! c) i0 F* O0 B7 s3 W

    8 A, K2 G4 R2 i1 U  E- Y7 N        This is where the function calls itself with a smaller or simpler version of the problem.
    ! R3 h9 k* @- t5 C+ A* p0 U
    4 q' r) [& c3 A9 P  M: F9 W        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).7 q+ ], x5 a! L: z

    : E6 n2 ~' d; YExample: Factorial Calculation* s: \3 ?/ ~/ H9 t9 D% W& J

    2 ^2 @  p( n2 `; B4 A6 @. iThe 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:- a: n/ O3 ~- b& F$ S. p
    % V  Z, c; j) ^/ _. b' h- j' |! X; K
        Base case: 0! = 19 y0 I! `' ^1 t9 A! x+ }

    # V$ ?1 Z$ h& z    Recursive case: n! = n * (n-1)!
    " f9 y/ ~9 _: ^9 n+ I7 p8 M
    ) F! o. S! A0 M5 {1 P1 l4 `4 O4 @Here’s how it looks in code (Python):9 A2 S( z0 g9 U$ S
    python
    0 T1 L- q  e( B) W
    8 c, m3 K* U3 K7 O4 M8 V1 A: }$ J5 p
    def factorial(n):
    9 m' n0 n: G) r! f# J# X    # Base case0 R7 o; m+ k' ~6 q& [/ \& ^
        if n == 0:
    0 r0 {9 p2 U. P& K# ?        return 1
    3 y; e: O9 p% W# w5 ^0 Q    # Recursive case
    2 H3 P$ U4 [3 H5 q8 d; N$ G9 A    else:( a1 v% X# f2 I$ J4 R/ H( a
            return n * factorial(n - 1)6 B) i7 }* {+ Y5 s

    6 e$ X# e- s& C( _" t& B8 Z# Example usage
    * M0 W* ^+ h+ E! e9 f* K1 ]( |' N; _print(factorial(5))  # Output: 1201 \2 ^# O2 D- F7 [, t9 J
    2 ^$ M* C) F8 @: w. n
    How Recursion Works# m/ A/ }+ @" M$ H& c! }
    ) I) x/ o9 m; F8 W% a* O% m+ T6 M
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ) E, ]9 o8 F' u- l
    * K% c& r8 t) T2 \    Once the base case is reached, the function starts returning values back up the call stack.
      m- B# ]5 d; o" B; w3 J6 j& n4 r
        These returned values are combined to produce the final result.
    8 ?$ f! I5 L' A2 w9 [- N
    % Q  g" L/ w/ a0 `& zFor factorial(5):+ \0 ]) ~$ R5 K* Q

    ( n) K" A4 j8 H/ n- ^. e  }) [! o4 [0 L
    factorial(5) = 5 * factorial(4)6 E0 G- ]$ @- d' N1 B( ]
    factorial(4) = 4 * factorial(3)
    , ~' I$ s9 b8 g( U7 L: V; I, Xfactorial(3) = 3 * factorial(2)
    5 h  J. g4 R+ L/ _3 l: Y: cfactorial(2) = 2 * factorial(1)
    " f/ _% H2 w$ S8 Ffactorial(1) = 1 * factorial(0)
    3 @# D$ c& ^& Ufactorial(0) = 1  # Base case! U, p0 z. e3 V% }8 B0 |& D
    2 ^% ?$ b4 ]; B0 D
    Then, the results are combined:4 n6 c8 Y+ X' e# j! [) ?  @
    5 k; r% ~* ?3 o. Z  ^; T8 o

    ) _) u. ^( M, Yfactorial(1) = 1 * 1 = 1/ F# a5 p2 y  S& b
    factorial(2) = 2 * 1 = 2/ N1 V. C2 a- g3 O1 R3 q% ?
    factorial(3) = 3 * 2 = 6
    9 W  c# W- S( X3 W/ Zfactorial(4) = 4 * 6 = 24
    ' R4 A/ \  @! P% J$ [4 }" }0 y8 l: zfactorial(5) = 5 * 24 = 120
    $ t; j$ ^+ Q- D; e' Q: o$ ]' t; e: a8 {1 r/ Z7 Z
    Advantages of Recursion4 C3 m6 [: F9 y' m9 [) T1 k+ t
    - v! z; s% v: g) {  ^  Z: g) Y+ j
        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).
    ( a! ]) r4 J% h6 s6 J) m$ s7 p5 J: {  d8 m) v% B; T; z
        Readability: Recursive code can be more readable and concise compared to iterative solutions.' P6 ?8 n7 @  ?; H8 C+ W0 W
    / v, o8 C" O4 P
    Disadvantages of Recursion6 v* g% [$ ^4 [8 O2 G
    " \6 d8 ]$ u3 E. I3 k, t- d- @, A: ~& H
        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.
    ' D' p7 v  ]" a: e7 ?8 ?- {9 `8 S1 j1 D1 Q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 t+ [8 {4 [7 i, U. J; }
    * P% j. l4 T; g6 K1 C: BWhen to Use Recursion
    - m2 y! e( o/ B/ k5 ^  w  A8 ]$ U1 `; |2 y, _
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # z! D! F9 X, w, U# v. l9 X; t/ M
    " @9 j0 C5 h7 N    Problems with a clear base case and recursive case./ {. b; S% J7 a( G
    2 z( C# t& f* v+ G  z4 }& [
    Example: Fibonacci Sequence4 t1 S' G. X* j  X* v
    6 a4 c- b+ \6 d
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 a" M8 g: P& u/ V7 w$ @/ X% [$ O8 u! ?5 E+ ~7 p8 p5 G( X( N
        Base case: fib(0) = 0, fib(1) = 1
    ! x1 y1 S: V& i  W3 I5 I8 ~
    + i& s& N- Q8 M4 U" O    Recursive case: fib(n) = fib(n-1) + fib(n-2)' m8 u' i2 _7 R" H$ z4 k
    : \. I& b$ T, c; F# {- z# }
    python
    3 G5 M; x6 F; @* B
    0 u, V# B/ y+ C/ i  s* y& [
    ' U, }5 c: O4 ]0 {def fibonacci(n):  \/ q* s: f8 o
        # Base cases* B1 _* E; `, P1 ~
        if n == 0:- ~# e5 J. V# _6 p. ^
            return 05 e- K$ g) T/ W" Q
        elif n == 1:
    9 W8 o  Q/ `1 X: l2 p        return 1; J3 `/ o9 Z7 ]
        # Recursive case
    ) N4 @) Y" X/ ~8 a8 x$ R5 Q    else:
    7 A* m$ o9 [8 g/ Y$ a- ^        return fibonacci(n - 1) + fibonacci(n - 2)
    ! T4 G% Z! H! L% Z+ X" N. n7 z8 s* y6 H* \9 u& E, t+ b
    # Example usage
    / ?5 q) T( d3 F4 B, ~( i7 Pprint(fibonacci(6))  # Output: 8) u; M1 ]) A! _6 u9 E& _2 M
    & t2 O' G4 N1 u4 `9 j
    Tail Recursion
    2 l3 f8 O- R' V6 k' p1 a+ m+ x- D  T6 N, r7 t/ B
    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).
    ) u1 E5 q6 I0 i# I2 D3 I) ?  n
    " i1 K; Y! E0 y# o: |. HIn 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-10 08:17 , Processed in 0.057366 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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