设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 4 K' v! a. x$ Q0 e
    2 M: \2 G( H- l% y8 u' d: j
    解释的不错
    % S, G: g- c* B! c) F
    - h6 W; V8 A% d  [  h+ h! ^3 e6 i递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。4 _/ v. t% _) D3 a8 Y* b

    9 e/ V) L" v- _2 f 关键要素
    + Q6 P0 x" E6 X1 f  j& J% K1. **基线条件(Base Case)**) h3 J# a! E+ W6 W( G5 p
       - 递归终止的条件,防止无限循环6 n: [9 F/ X( D+ i! f4 M% f
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    1 G6 N" W/ r7 z$ }" g3 a( G3 k
    / w) X! T- `( \. S2. **递归条件(Recursive Case)**# t9 x! g# U7 A' G6 p, k4 Y
       - 将原问题分解为更小的子问题
    5 L$ T& @$ g' q, O  w) u   - 例如:n! = n × (n-1)!) C: v# o! n2 q( @7 t7 C

    : k# w/ ]1 P* u3 V5 q1 v% c 经典示例:计算阶乘
    . {+ C& L5 F7 F0 t9 L  E& U$ Spython2 M" U6 D- R; N" v9 t# Y# f7 B* F4 W
    def factorial(n):1 X" X- v6 z6 h/ f/ N' F
        if n == 0:        # 基线条件
    ; e' ?* {& t6 @% y% v  z( L3 q' Y        return 1
    7 z; I1 V4 n2 f/ [* ~8 w    else:             # 递归条件
    $ {( t6 H, `! f( L% u! ~1 Q' d        return n * factorial(n-1)
    * q) \( B5 |' J. m5 e- i执行过程(以计算 3! 为例):0 M/ g( w9 a$ X5 ]1 @0 ?  ]
    factorial(3)# h3 d1 ~) [( c5 e2 h9 l$ q* `, m
    3 * factorial(2)
    , h1 t1 y8 c, p- n. X. h3 * (2 * factorial(1))9 _" H9 h5 C5 @
    3 * (2 * (1 * factorial(0)))8 v& n. M5 I' o- P# h7 x0 _
    3 * (2 * (1 * 1)) = 6* e5 N- l- M3 k; u
    8 z. m/ t7 ]! M
    递归思维要点
    + w" c5 h" P/ w2 C  H9 x1. **信任递归**:假设子问题已经解决,专注当前层逻辑  Q6 J0 l1 _/ S* l! \. x6 P9 l2 @) N9 o
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)4 E, x7 [& w5 @/ N( K. ~
    3. **递推过程**:不断向下分解问题(递)
    0 P" }5 j, T$ f/ j4. **回溯过程**:组合子问题结果返回(归), A8 T. O) u$ C" B& G/ a
    3 i0 ^2 P8 J7 c# N# @) j5 D
    注意事项! p' {) K( r2 R# O5 U2 W
    必须要有终止条件0 X% N3 k, P* _" f7 ^  q' `
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ; I" @5 [; b% ]: X( f! ^. t某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ( z, Y0 j; \& @* }; n: R. t尾递归优化可以提升效率(但Python不支持)
    7 I# O. z' ]" P6 w
    % Z( Z1 S" @4 a 递归 vs 迭代
    , ^* M9 t5 V" e|          | 递归                          | 迭代               |
    0 p7 L+ {3 k8 O" k# ]- i5 r- G|----------|-----------------------------|------------------|
    + t" q, }: A: P" w, `4 n% z4 o! l| 实现方式    | 函数自调用                        | 循环结构            |
    + `1 Q5 e/ K! w/ f) g2 H1 F' d- ^| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    3 Z. V+ g  ?, ?, I; w| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |, G$ k! T3 Z1 T) E
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |* ]9 L  A. I8 ?

    - F6 [4 {6 z% x& g' k 经典递归应用场景, o5 w3 K0 v  N: _6 M% W
    1. 文件系统遍历(目录树结构)
    / x6 v9 M. c2 M% P. L( c2. 快速排序/归并排序算法; a. U, H# X4 {% N4 r
    3. 汉诺塔问题  U/ s1 Z' ~  \, x. @
    4. 二叉树遍历(前序/中序/后序)
    / _4 B( c% O/ z  f1 c" \5. 生成所有可能的组合(回溯算法)
    $ N" P! Y' a2 @( p% ?1 M0 y
    ! z0 N+ G" W0 X试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    15 小时前
  • 签到天数: 3103 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    $ E+ t! `1 l  M* y) g5 S我推理机的核心算法应该是二叉树遍历的变种。5 W8 Z$ G" j: o6 B
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:( q  v4 ^6 p. j6 x
    Key Idea of Recursion
    + u7 ^2 y" O# r2 @- f( N& A, h) L/ J( h: M
    A recursive function solves a problem by:
    ' T/ |( {% J$ g& O' W
    : s. l/ M  X, ^! }7 K: [/ r    Breaking the problem into smaller instances of the same problem.
    3 x: _) ]6 c$ S( O; W! R. @6 @- h2 y" W* K7 a7 k# b, Q! S
        Solving the smallest instance directly (base case).) j% J- P, [7 e: ^; f" E2 t
    - O( {0 x9 f9 ~1 h  h
        Combining the results of smaller instances to solve the larger problem.8 W; R5 i9 P! g/ c" M9 p9 x9 o

      Q, P5 J; H! m, g& zComponents of a Recursive Function3 s- b' V1 L5 Y: j. S

    ) |& V5 J3 \- ~( y* m    Base Case:
    7 D) e4 q: I( g& T4 p  R8 t
    + t  {9 Z! ]  j( K" q        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    % n5 g- g/ ]* Q' s$ K6 L$ f0 k6 B( c  ~( W8 B0 L
            It acts as the stopping condition to prevent infinite recursion.
    ' O! h; e0 E1 g+ ]
    % k: o7 z8 U7 g  Q8 W0 M        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    8 R* Y3 S: `4 b" j4 J+ M( u& s
    : C" B/ ?5 u' {* D  v    Recursive Case:! x" \( Q; b2 Z6 Y
    / D( E- t* s- ]' G4 L, N5 i% _
            This is where the function calls itself with a smaller or simpler version of the problem.
    ) _6 Y. g  x0 M* |; U, U6 X) \; t  _2 h
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).; ?% x- n$ E- n3 R" o
    : k7 h* l- S. [9 x7 v2 K5 d; e% O
    Example: Factorial Calculation) c; x  ^8 r2 o& x# o9 |5 t  `3 l

    / E6 j- e3 j+ c' X* q! H7 j4 eThe 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:
    ( I1 X) i3 h: R2 Y8 Y7 g) |2 _
    5 s; e9 V- U% A) i0 x2 p6 ~    Base case: 0! = 1
    , O5 K$ M4 V: g% I, |- S: g; r  g7 w/ v
    & J* u+ g0 [. S; I* Z    Recursive case: n! = n * (n-1)!
    ( y5 @6 f8 x+ r& I- {: n
    ) d: d9 w/ }  LHere’s how it looks in code (Python):
    % T& s' ]# J* T( J0 c. J: Q0 Bpython
    # f  n+ R% X( ^, h7 L
    8 K3 O( I+ h  O/ o- Z# o
    $ R  d) S/ U5 ^. e* z5 T3 P* n# ?def factorial(n):" f& _/ y- |& j: D
        # Base case
    3 P4 t, R- [. A5 n    if n == 0:; v! M' q# f7 j$ x3 g
            return 1
    # @& G% V# E2 C4 k# _$ |    # Recursive case3 ?# h- I1 P! u$ @; ~
        else:
    2 X2 E3 ?. q) I" Y3 L( t* d        return n * factorial(n - 1)/ u2 m. L7 g) K" G
    5 O$ Y6 n+ c: }- y
    # Example usage
    ; b1 j* D  {6 z" h& |print(factorial(5))  # Output: 120
    5 h0 Q/ B6 k0 b% D+ K
    2 X  d1 T0 \0 @+ q; I4 F  UHow Recursion Works- m. d1 m: \4 o  \9 h, z9 m
    : Q# t6 P5 b' y6 x3 V
        The function keeps calling itself with smaller inputs until it reaches the base case.) G0 V$ Y, o/ X5 s* \8 H+ z' b

    % Z/ Q( G% `* q    Once the base case is reached, the function starts returning values back up the call stack.
    ) `- }2 h* A9 n3 K0 p
    + p* |8 q: B8 j( ?    These returned values are combined to produce the final result.
    5 h' `2 I! @# v! D7 b. {/ V* g* |) \$ j: o8 t$ e! \
    For factorial(5):
    " R! D- Q% _- I; q' u0 z
    , @, A8 N+ Z$ _* ^5 ?+ B; Q' S# V1 Y0 h4 u% V  O) s! ]
    factorial(5) = 5 * factorial(4)
    ' h/ M0 N) D- i6 Q. [3 {* @! h4 E7 Rfactorial(4) = 4 * factorial(3)
    - |( z) V% a0 z% x7 v4 Ifactorial(3) = 3 * factorial(2)+ p$ f% X  a  _' ^" M, N
    factorial(2) = 2 * factorial(1). M/ @' W0 w: ^4 V1 @9 x
    factorial(1) = 1 * factorial(0)
    ) |( `3 G$ O) cfactorial(0) = 1  # Base case
    & v$ I0 c' F+ d" G6 B8 ^
    4 j/ T4 C, P/ A8 i6 }- n9 e7 D) E: CThen, the results are combined:
    1 z$ S5 ~" ]! d( x9 }
    7 p. V$ w. J6 L- |/ f* O/ s9 s# V# n7 }  X, ?
    factorial(1) = 1 * 1 = 1, h/ J+ u! J( j5 W
    factorial(2) = 2 * 1 = 2
    2 r/ V# \- U" jfactorial(3) = 3 * 2 = 6# R8 b* Q5 E4 x. W: n. J
    factorial(4) = 4 * 6 = 24
    " J' Z4 O+ Y$ V! b* |! efactorial(5) = 5 * 24 = 120
    ( P& a4 t8 v% S! j3 {  _
    : K' R) O4 A. v( T2 ~Advantages of Recursion- U' B' u% S- C/ t

    3 L6 \: ?7 w* `; C* _    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).
    - ]5 ^2 N! g4 R9 `0 z
    ! Z7 b3 Y, F' D% b8 i, F$ G+ g    Readability: Recursive code can be more readable and concise compared to iterative solutions.7 r/ |4 X- u+ s1 T

    : H5 O& q6 J3 v* N8 R' XDisadvantages of Recursion" \5 Z$ ]$ X+ W$ O

    8 _$ u$ n: J9 f5 C  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.
    7 D# Q0 k2 s" ^
    4 {3 X2 T; n# J; ?- k3 i    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . v7 J$ G' t* E; h! F' a* M8 |& x
    2 J( r5 a5 q2 G9 wWhen to Use Recursion: G7 O6 B4 a- R& j: u# D

    + q' n& V% y6 m- M7 ^9 l    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 N7 U; F/ s$ [- h2 W6 X5 D2 D5 s
    ! Y! @) X+ Q# N0 m  J
        Problems with a clear base case and recursive case.
    7 c7 R& ]  }! f# h) n; R8 O' B, v& Y& m$ Y
    Example: Fibonacci Sequence7 R6 r. ^1 }7 p% f) _; P. L' u
    ( Q6 J+ ]# Z0 K- t
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) F0 a: G) ~& Q, U9 x' j
    / s2 L7 ]* k! [1 F" y0 Y
        Base case: fib(0) = 0, fib(1) = 1
    / T8 e+ n& o, c6 b; M9 p$ Z2 G: W. v2 `2 p5 H* w
        Recursive case: fib(n) = fib(n-1) + fib(n-2)% Z. G/ l' K2 I) `" k/ P- K
      D' I" J! k+ g( W* V9 S
    python
    0 d/ k- P2 U" h5 {* N' s- f
    & n' ], g4 I4 y9 y9 N+ y* y1 X& H& {9 p( ~' s% ]# M
    def fibonacci(n):, Q3 E: I) z: u' w, g* M2 Y
        # Base cases
    ( n6 S. T  z0 v    if n == 0:+ @; d, b) F  C  O
            return 0$ g$ y* p2 m& [3 t0 Q; i
        elif n == 1:
    2 w& N2 m7 C  U3 K5 C* |, V        return 1
    . T' {' b& R& d" |5 x, x* u* m    # Recursive case
    , q" O6 t; C2 {+ }- Z# W3 Z    else:9 \7 |0 e/ e: b4 V: }4 S& I. _! I
            return fibonacci(n - 1) + fibonacci(n - 2)( l* P- e7 [/ A% e' i5 r$ t. A
    / z4 T0 B( m* D  P/ x! {
    # Example usage
    % o" q& t8 y' M! G. Z1 f0 s9 c0 `. E/ _print(fibonacci(6))  # Output: 8
    3 v( g- h' X* G  c! `
      j7 U- u0 j5 W$ m+ Y6 _Tail Recursion% Y' M0 q! d  ?9 I

    6 B$ H% {& k4 @3 o" X/ ^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).9 \& U1 b4 [/ B# K7 e; O/ @
    ( L$ \: X/ Q9 F# C; E4 d) S' ?
    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-28 23:13 , Processed in 0.029149 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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