设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    $ P: O. z$ u" B- G
    % }  n6 j4 X$ W' _% ^解释的不错
    7 F/ ~6 Z8 e- m: c3 g8 H/ K- H; G% w$ p; N& X
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。: Q: \- t7 [8 c* Q$ K4 |

    . C7 B/ H" w$ W$ w; X 关键要素
    - S( X5 c3 A; }  k) i- n: D6 k- M1 f, f1. **基线条件(Base Case)**
    ! k2 d, r0 |+ z   - 递归终止的条件,防止无限循环$ K6 u8 z: V- k9 u# o/ d$ Z! U
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 [. ?, k& M" k! ^/ }+ y# O7 s$ }

    " ~7 Y/ u2 K- @4 [8 R2. **递归条件(Recursive Case)**- \: e5 R5 q0 d4 [0 s
       - 将原问题分解为更小的子问题
      I6 n4 D0 ?3 \2 u/ D9 E7 C0 x   - 例如:n! = n × (n-1)!
    # R+ c( |# h8 |2 }/ U* k# w3 \
    3 |) m' j/ j. ^) ?( N! n! G 经典示例:计算阶乘' }( \& m- l- K- s6 Z9 H4 `
    python( l3 l) T5 C1 b
    def factorial(n):+ f( }: L3 Y/ W2 J- Z9 p# X
        if n == 0:        # 基线条件
    5 A/ |+ I+ ~) O! A9 L! S4 h) Y" G        return 1
    3 U" j8 u1 ~0 y1 {    else:             # 递归条件
    ; h9 W2 t% S1 P. d8 @        return n * factorial(n-1)% c' E# |9 Q- ~3 ^& k1 h
    执行过程(以计算 3! 为例):
    % \5 u8 {8 j% ifactorial(3)
    2 I" a% D, c9 ~# @# j# A8 {& Z" N- o3 * factorial(2); c4 f; a7 ]- w! J: m& I8 b
    3 * (2 * factorial(1))
      `+ U% d  K1 y; d2 q" q3 * (2 * (1 * factorial(0)))- x  {$ m, |" k) l; k* ]
    3 * (2 * (1 * 1)) = 6
      E6 y6 z+ F+ P' ~: ?' K+ x, c( |/ }& G0 U5 h' @! j; L
    递归思维要点
    + q5 k1 I, v2 e" Q4 k+ K1. **信任递归**:假设子问题已经解决,专注当前层逻辑5 Q( v% B8 Q# A: U
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! k9 i/ S' c5 r1 v4 p& C
    3. **递推过程**:不断向下分解问题(递)/ {0 J+ ^, ?0 l4 }5 b! Q
    4. **回溯过程**:组合子问题结果返回(归)
    + `; F3 _6 c( Q3 j& I, C3 w" \  t- _2 ~4 R
    注意事项
    5 }& U& n3 c, X; }8 L* S必须要有终止条件& g  `$ Q2 m  I, v, D
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    # T! ]6 ?7 u' d: Y某些问题用递归更直观(如树遍历),但效率可能不如迭代
    $ D  i' s; d1 ^8 f" g尾递归优化可以提升效率(但Python不支持)
    ) z! h) G* P! U, R
    8 L9 B5 a# r  _! G3 F6 J" U 递归 vs 迭代9 j! n, z1 I. f# p0 Q; A$ V
    |          | 递归                          | 迭代               |" p4 t) z5 ^4 i
    |----------|-----------------------------|------------------|
    8 x4 r; a) q4 a| 实现方式    | 函数自调用                        | 循环结构            |
    ; T, z) R3 e- f5 C* w% `8 n" J9 t2 N( E| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |' P- d2 v1 _; K1 }
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    9 f5 P) G2 e# f/ z| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ( y2 b- ~( x: {2 N- U
      O5 D, z3 M1 N, q: S! W6 M 经典递归应用场景
    # N* T: l( {$ S: U6 S1. 文件系统遍历(目录树结构); n" Q  l+ w9 s8 G7 ?
    2. 快速排序/归并排序算法3 f2 [) g% P; g% ~1 ]! N
    3. 汉诺塔问题
    ) \- A; N/ l4 G$ o4. 二叉树遍历(前序/中序/后序)" U9 F1 O+ k5 w% ?9 a% }4 T
    5. 生成所有可能的组合(回溯算法)
    4 D' R1 F' F  ]$ o8 v1 h1 z+ f) k4 u2 j" H
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    8 小时前
  • 签到天数: 3107 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( ?. u2 i4 C* S8 a4 M7 e我推理机的核心算法应该是二叉树遍历的变种。
    * f) s' m, y, C8 R另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:8 N3 j4 \" w& m/ V6 M
    Key Idea of Recursion
    0 q8 J" F5 n( T$ l+ M0 h) v0 t% R
    ; }9 P( q& D5 a  v( k& X( YA recursive function solves a problem by:! f  I, a: t3 A. c4 S2 q

    * C1 Y  |! u& L% B    Breaking the problem into smaller instances of the same problem.
    5 }8 S/ S1 j; Y" ?8 Z) Q+ @, G$ e) E4 D) p3 G
        Solving the smallest instance directly (base case).
    . o; J  U2 s0 |* H& ?& S4 {
    6 G& {# S  k, _9 [  ~3 [    Combining the results of smaller instances to solve the larger problem.
    6 p- M: i* R, H5 m* C$ i
    ) }0 O, y7 ?  s/ o% ZComponents of a Recursive Function
    9 B2 I) ?; D% k9 p% s" N- B& z  r8 M2 V# o* R; R
        Base Case:
    $ q. b! a" r& \' [1 m  I
    ' d5 |2 h% N( B" Z  L! h2 {9 c        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.  z. I  w% X( Z: s7 p5 y

    & ]/ s# B* ~. P! j! F& Q- h4 z& B        It acts as the stopping condition to prevent infinite recursion.
    6 V) w6 j5 `  T' g$ Y( d6 _9 I) h0 q; y  r
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.- C& X* e9 A) x* }! o$ Z

    + _8 L& e+ `3 |" q+ ]    Recursive Case:
    + N$ [5 s; m5 k( w, B- R6 {. w, B+ T% F; J. O
            This is where the function calls itself with a smaller or simpler version of the problem.4 \# K6 w" z  C: A4 w
    6 r7 L- C& ^1 m' ~$ ]9 m* g) v/ e
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 G" @: @5 U2 M( f
    5 E; Y# u; m, D4 J! F1 V
    Example: Factorial Calculation
    - O0 E0 H6 r! c; p- l) C& B9 k% ~; V2 H& f* v
    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:
    . g& A4 v! c* B) y* \) y% Q% i7 v
        Base case: 0! = 1
    2 B. \, b! Z% `5 n. S- {) J' u" I+ X' x2 Z9 n. Y5 {0 c3 {
        Recursive case: n! = n * (n-1)!, S0 T: x+ F1 ?
    . W4 a) X9 c' y/ {2 x
    Here’s how it looks in code (Python):" y+ G  @# t$ N  l6 `6 e
    python
    7 M6 e2 [* \& {/ B; T3 l6 d! l. B- s6 h

    ' H) M6 _' @4 qdef factorial(n):- ^" }/ r5 \0 _- ?8 i2 S7 d
        # Base case8 H0 b; h' b3 Y$ g" z
        if n == 0:
    " ]$ _! i; ?' n9 }  G+ L+ ?        return 1
    . l) J5 [5 L/ ^$ v; @$ d4 E    # Recursive case/ u: ^- {+ k4 i) x
        else:% z+ C6 S6 \! M% J: [
            return n * factorial(n - 1)
    * T- e! X; v$ @; ~) R, _4 H8 Q# Y; o# t6 _# R# Z( L
    # Example usage, C& {9 i, E- J+ [8 b, e  X  K/ K
    print(factorial(5))  # Output: 120
    3 K1 `" d% A4 |0 M
    0 l1 s; I3 S5 xHow Recursion Works
    5 l. |1 h+ p5 U% J4 e* O7 h' I, L& ~
        The function keeps calling itself with smaller inputs until it reaches the base case., \6 M" X- Z% I& G$ d. e5 R

    / F2 F4 G. F& Q7 y  \) ]    Once the base case is reached, the function starts returning values back up the call stack.. V' d( W$ \$ P
    8 C" Y- d! K% X3 _9 X7 J7 R
        These returned values are combined to produce the final result.
    # A. I$ c! F6 T  v
    ) v9 D0 n5 h7 bFor factorial(5):
    1 c- F! U/ T. E7 x. ?: U5 C4 F! t
    9 @* g+ q" r2 r: _# m
    ; I: L" m/ U. Yfactorial(5) = 5 * factorial(4)
    4 k- {: E  a' ?! d8 ffactorial(4) = 4 * factorial(3)3 l. P9 `% K1 ^
    factorial(3) = 3 * factorial(2)
    $ k3 |' m  b8 t% Sfactorial(2) = 2 * factorial(1)
    # K5 v3 \  H! x# J2 K& {factorial(1) = 1 * factorial(0)+ y& X" g% Y8 B0 _
    factorial(0) = 1  # Base case
      r0 N( o* J5 \7 l
    - i4 B* z; t. CThen, the results are combined:
    6 \3 ^( C" T' v1 J
    # n4 X3 R% V# A) x$ d2 {. o4 k% d; j% G* P8 E! s
    factorial(1) = 1 * 1 = 1' r$ q1 z, I$ z. _0 C& ]
    factorial(2) = 2 * 1 = 2% N  b6 x1 Y9 H
    factorial(3) = 3 * 2 = 6
    6 C9 u; r! }; e, l1 H! F" k. Pfactorial(4) = 4 * 6 = 24
    ! ]/ @0 G# U! M, z1 {) kfactorial(5) = 5 * 24 = 120) r1 F, q2 z- C7 q

    . S% u5 j2 P) w6 zAdvantages of Recursion
    . C' Y' j! `# F# Q
      K9 |0 f8 {: I+ b' |    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).7 C6 O( T( s4 t0 U; E6 R! F

      M, K) Q3 Y0 }5 T: s    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    5 o7 u) s8 \# N7 o5 o, E; Y" z
    5 D/ E& J  L! q: B- N7 tDisadvantages of Recursion1 O3 X& L* ^! B! x; B( ^' j" ^

    2 h+ L) @2 z2 u    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.0 X5 g0 c4 G8 N& K9 k! b$ d: B0 R
    8 L7 e  [+ `/ [8 U
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. C$ }4 `4 y; W3 w, l* g2 C+ m# V
    3 }0 Q1 F$ O! U# L) v6 y
    When to Use Recursion6 m5 C' e% Y, `0 x, a9 v& A
    1 n! S6 M, z  o4 `  ]
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# s9 k# s1 _- x  U; O+ i1 M
    9 q8 P( t3 Z  e7 K  h1 z* Z
        Problems with a clear base case and recursive case.  h" f. F4 Q9 {+ h. ?( V- r4 w
    - Q- `. t8 Q5 v4 y$ s. i; f
    Example: Fibonacci Sequence
    $ G+ p+ P6 D' G4 z& {' ~& L2 }/ `3 _9 R
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 ^% [2 r7 i; ]  _
    2 L2 W3 F6 n+ `' i7 e    Base case: fib(0) = 0, fib(1) = 14 J, L. y  i0 }  g

    - O& B' I% L% _1 G# R% L$ c# f9 p    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    - [7 Z  c3 \5 ]3 [2 ~. z' a% _4 d; w8 y( s
    python! ]6 \) Q% p1 U
    ; e, w1 ]5 T' v6 `0 L! p: b

    # Q- b2 a& A0 |. [3 E0 ^def fibonacci(n):
    8 T* V# @4 x4 R3 v( a8 X    # Base cases
    ( x) A; N9 p  d/ }3 g7 ?$ u+ M    if n == 0:
    5 a3 C% }" N2 v$ \5 l+ c, f% N        return 0
    9 r& _  B& a5 Z( j8 A6 f$ w' z    elif n == 1:
    0 ?$ R7 g$ [3 V5 _% {        return 10 P! ]2 b/ Q4 f5 K1 U6 Z4 E' ^
        # Recursive case
    3 z0 J9 ^2 K6 k& ~* u& Z    else:# a' H4 r; g$ N$ d& Q, }0 W
            return fibonacci(n - 1) + fibonacci(n - 2)
      P+ L# ]! x1 h  k0 h: c% q" S. `, I$ c+ _. t; e1 m! {! ?
    # Example usage
    - w) z1 C0 l, {% Y$ d0 D4 cprint(fibonacci(6))  # Output: 8
    ! l& j! _) E6 }8 o+ }% K- Z9 `0 l, W, T6 `' n* f
    Tail Recursion
    $ W" E! P9 Q( d0 G- g( Q# t: O1 F1 \; A9 k- h
    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).  v1 _2 x8 V' J1 ^
    * i( F& }( a- v. V0 }5 J6 h/ }
    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-12-3 21:13 , Processed in 0.031798 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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