设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % V. Q1 m1 w' U/ ]" P- \$ l

    + B8 v; }. h. C' w+ g+ V. R解释的不错# F: {2 d6 z0 a
    : t3 b; m2 y) M" @: R2 R
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 w: B" i8 k2 N
    , h# s8 z2 O5 i* ?1 v! e 关键要素
    # f- R2 c' D9 ]: E3 n, o) M1. **基线条件(Base Case)**
    ( X' w" C0 o. A$ _. }0 ]- c8 O- {4 R   - 递归终止的条件,防止无限循环
    & t1 m2 R* S5 C   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 15 P! M, _: P. D, ], u; ~1 i, j% @

    2 v& q% B7 J+ _( U- q8 }+ e, }# ]+ j2. **递归条件(Recursive Case)**# D! X6 U. y1 R# G+ b
       - 将原问题分解为更小的子问题3 n  r. e" Y: H5 T1 U: W8 ^
       - 例如:n! = n × (n-1)!
    2 n' j  @' o: }& r! j5 g* D0 r* r, a, l' ^! `0 U8 F1 P2 y
    经典示例:计算阶乘: _. \% r" |  V; d# F
    python
    1 [+ \: C' |% R0 `def factorial(n):  D% e/ i3 p7 o/ Z/ w0 F
        if n == 0:        # 基线条件
    ! s# z2 h- P& Z) T+ K        return 1
    : y9 \8 d) m0 X    else:             # 递归条件
    . B. H4 \$ H4 X# [% a        return n * factorial(n-1)2 r* h# S! C) N  @6 l5 O- @
    执行过程(以计算 3! 为例):
    ; t" S2 o5 K5 A7 w; ]0 h8 \factorial(3)
    0 {6 E2 Q* l: p' g3 * factorial(2)7 h6 Y& `) i, F# g
    3 * (2 * factorial(1))! V: y: }$ |! F4 D8 e& _6 S
    3 * (2 * (1 * factorial(0)))- D/ e1 U; G0 O% k
    3 * (2 * (1 * 1)) = 6
    7 d# J0 P$ T0 u, A4 O
    8 b+ y2 _( |. z5 J4 t" T 递归思维要点
    0 U$ p! e) a& }7 g' s1. **信任递归**:假设子问题已经解决,专注当前层逻辑. M! {9 c+ l, w# W% a" n
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    % Z/ e6 Y+ p+ U3 P! N3. **递推过程**:不断向下分解问题(递)
    ' P7 ?2 F0 |5 x+ c1 K4. **回溯过程**:组合子问题结果返回(归)
    5 j) j3 P) F* O6 d) Z
    9 A8 K6 V+ e" ~' X9 Y+ H 注意事项
    - q, ?7 f3 u" p$ L5 x; X+ A必须要有终止条件
    . j9 b  E2 N+ P" w* ~递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 n7 h7 \7 N' A' E8 @$ ^/ A某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " G3 n! j4 O7 S6 Z3 x尾递归优化可以提升效率(但Python不支持)
    7 ]% I( D+ d1 H, Q+ I( k  K# g2 `6 h! |7 f
    递归 vs 迭代0 [) H7 v( x5 q2 @6 w) A$ x
    |          | 递归                          | 迭代               |4 j7 S" @" {- I" q8 G
    |----------|-----------------------------|------------------|
    ' D: w" v7 c( E2 \5 f4 j$ f- F& `| 实现方式    | 函数自调用                        | 循环结构            |: d) b7 M) l$ U
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) C  `$ H: b8 E$ k/ F5 U
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |  ?! ~; t1 i; X& N, L
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    4 \, T7 }4 a! ~$ d* h
    ( A$ h! A9 j) s7 F) D- I# [ 经典递归应用场景! K* b8 d8 f8 l4 a( g7 V6 ]3 `
    1. 文件系统遍历(目录树结构)5 G3 n! V* g8 s* g
    2. 快速排序/归并排序算法& \9 A: b& u" W; u' Q+ R* x
    3. 汉诺塔问题5 N# ], l# z" G9 o  p* ^$ i& [' P4 ^/ ^$ x
    4. 二叉树遍历(前序/中序/后序)( ], G) L6 e/ E+ i
    5. 生成所有可能的组合(回溯算法)( h3 g8 }. S8 b+ E0 `; |1 e

    ! J  _9 k3 A7 J  p# e试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 13:27
  • 签到天数: 3116 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 V) a( z; O6 k/ t5 C' u我推理机的核心算法应该是二叉树遍历的变种。
    6 W1 U, @8 a- [2 M另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 k$ B0 C4 H5 Z4 O
    Key Idea of Recursion+ p# ^8 a5 a/ z2 w

    0 p* [  B; W7 u& o3 o3 C3 \1 U" nA recursive function solves a problem by:. ~9 ~/ J* a3 T2 I+ f3 u

    + B' X9 |/ ], n4 ?    Breaking the problem into smaller instances of the same problem.$ x& N. Q$ B4 \$ j

    3 k9 M; J+ N8 N+ ^5 S5 q    Solving the smallest instance directly (base case).
    1 O0 t7 q! X2 c0 A2 ]
    * L1 h( O; d1 @    Combining the results of smaller instances to solve the larger problem.0 }9 A# O" g) Q% L* j( ^& W

    4 ^) J7 h% E+ ]Components of a Recursive Function
    6 @- a% D# z& O+ \! \, n* ]! Y& [2 L1 A5 {! X: i& P$ q- V- r
        Base Case:
    7 O) h: N8 D+ b7 q/ E8 q
    2 V7 z6 C" s  r, |! o        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.) v* G0 p- ~2 {* v" R7 d
    ; J1 [" ~# D" @7 P" p
            It acts as the stopping condition to prevent infinite recursion.
    ( t  f: n% Q3 [" ~2 G  v, e9 `0 b1 ~. Z) C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.: O5 y- D1 A0 \
    9 q& Z. m5 k3 |
        Recursive Case:
    ! ~$ W# v$ N% s6 c; ]4 S
    9 @5 m0 O% U- p" R3 ~        This is where the function calls itself with a smaller or simpler version of the problem.: H' r& {! s& Q7 C. h

    # {' P: L- n- X2 l- i* N        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- @& f  o; h  h# Y3 z/ a$ w, Z

    , c8 d8 J8 w$ z; d* ?: fExample: Factorial Calculation/ H$ [& Y& L  r' ?( T4 m
    # f0 F' W4 X3 ?$ Q  o$ a, G# L
    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:/ K+ S1 u' \/ [7 w
    0 Q% U0 u0 R; |+ }* @7 q0 f' d
        Base case: 0! = 1
    ' y0 R4 s+ G* g5 H" {
    ! M. r" O( K: t; N" J    Recursive case: n! = n * (n-1)!7 ]  \( `( d+ r1 B+ f
    , t% ~( J0 B3 Y& I
    Here’s how it looks in code (Python):( V3 g" B9 L' m, _. Q5 H7 x
    python9 I8 N3 B% a, x+ n7 b
    7 j; y3 i" L+ Q/ d
    ; g8 p/ q) D6 ~3 k, r
    def factorial(n):
    ' l5 ^. b$ k% K' ~" ]0 C8 Q+ B    # Base case
    * q, H! \! g% u    if n == 0:
    3 r% k" G; x( Z2 i% s3 G        return 1
    9 A# Z! e1 M3 E( z& k& z2 F2 @- x; R    # Recursive case
    / \- _8 _3 g9 `- M; c) R    else:
    $ c8 o2 l; z: i+ W0 @% S* L" [7 z        return n * factorial(n - 1)
    : z+ g% I7 ^9 }. D. ?' h( {
    6 D. j" S# z- ]! G  Q" b# Example usage- I- J* \/ p" Y2 L1 h) R$ D
    print(factorial(5))  # Output: 120
    : p* N: o$ V3 U! w% `- x$ H5 T
    How Recursion Works' a0 ?: F* d' [& c% W
    + j2 {/ \6 D4 {9 R
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ( c1 ^( |- Q- G6 q0 _: m
    + I- Z- v% u" q9 z) Q3 f    Once the base case is reached, the function starts returning values back up the call stack., N$ h$ C) h7 V2 ]! ~! ?3 F

    9 j2 L, s. C  F: L% o- Q    These returned values are combined to produce the final result.5 p- x: U+ {# a  S' o5 f

      W* |2 N, O9 U) y& A7 l0 fFor factorial(5):
    5 p/ ?1 J5 C0 w' q$ @! S/ a
    5 D) x1 Q: N" E! d" S6 q5 X1 h! @* [% h1 r/ K
    factorial(5) = 5 * factorial(4)
    - k/ v& X( ?# }2 J& `( Bfactorial(4) = 4 * factorial(3)4 _% J; Q8 J- P/ u$ m5 D/ u1 v
    factorial(3) = 3 * factorial(2)
    2 X# q. Q1 o4 z! B% `* Tfactorial(2) = 2 * factorial(1)+ O3 p% [1 N% ~+ Q8 A
    factorial(1) = 1 * factorial(0). B8 \1 t  Q6 ]3 F: Z% ^# g. w
    factorial(0) = 1  # Base case
    1 |; ?7 E: t- l( ]
    : N* W  `! A1 V8 w) hThen, the results are combined:/ b) m7 l5 ^  l2 r

    4 p9 O  q1 d, c# e+ o; T% Y- D/ w, d- N" o$ H6 \
    factorial(1) = 1 * 1 = 1" C9 {' }7 K. g+ p& [. e  ~" B0 b
    factorial(2) = 2 * 1 = 2
    $ B- K- m% p( D" @factorial(3) = 3 * 2 = 6+ S) a5 s1 ]: b1 Z9 ]
    factorial(4) = 4 * 6 = 24
    , ]: |$ F9 \# \9 B( Nfactorial(5) = 5 * 24 = 1207 D; K  J4 C' R, w
    * [. y1 j9 x/ ~
    Advantages of Recursion
    8 d7 u5 k2 c- [; ?, }+ o$ V
    6 `& d$ C% v+ ]' K3 W1 i    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 Q3 m+ f- Z( b& W5 J
    $ _+ A7 k) b5 f9 [0 Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.& {0 d5 ?7 W" T: a& `5 f/ R8 Z
    6 \4 H, L* B, v
    Disadvantages of Recursion/ A) ?7 B# B) G4 D! O% z8 g: f
    8 V! C; N* }5 g- b
        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.
    : e! ]0 m, r% j  \, s  L) s7 f& [) x* i2 o9 I. A7 g9 h) [
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - l' M& O2 f  [, A
    . h" l( O4 r  g. K$ m2 EWhen to Use Recursion# R+ ?$ x  Z' z" b9 A8 M
    9 h- z1 i2 {  M* ?- C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).: ~+ O- h, K! _$ c6 \1 b6 j: K( x

    $ Q/ {! F; [9 n- i3 ?    Problems with a clear base case and recursive case.
    6 I6 N/ {$ U/ a( E7 K
    / K* y4 K* s$ L$ |9 C* LExample: Fibonacci Sequence& J# k# b3 I6 u, a: s

    & e9 c, A4 P# s, Q! uThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:3 b- n" p$ V6 h
    : @  X% I& N! S1 i% S5 x3 T; W
        Base case: fib(0) = 0, fib(1) = 1, [( }! b6 l+ }: y5 S

    5 b% u1 U% y! ~5 @( @$ w    Recursive case: fib(n) = fib(n-1) + fib(n-2)5 E( F; ]5 T& ]* E+ W" U8 G

    . j0 f1 B( d% }! ]2 Q  m6 Dpython
    6 w' \. b& n# R" l; e( T5 A& q
    + l/ y) X2 r" [7 f
    + ^9 g, ]! ]: x6 s* ^7 qdef fibonacci(n):
    - C( l9 ~9 u2 R9 g4 Q2 L    # Base cases
      B: Y$ b$ \% {8 [- n% W  y    if n == 0:
    6 O4 S+ L+ E0 Q+ A+ z2 |  c        return 0
    5 r/ w5 t; m9 g; ^    elif n == 1:
    . D- e" b# p2 o: t        return 1; B$ P3 g0 W! e. d" `  T! ~9 [3 |4 z6 a
        # Recursive case
    3 {% O1 h7 l- T9 s& [9 @9 K' ]) n  J    else:
      Q" A' \1 V! O7 _+ z        return fibonacci(n - 1) + fibonacci(n - 2)7 S( w3 l& ?  E3 I
    4 m6 |, s4 Z% {  G! N2 |
    # Example usage; ~2 S6 |% N$ x. i8 y7 f
    print(fibonacci(6))  # Output: 8) r$ n$ d4 d) f. y

    $ W) W+ M* Y8 L3 i) DTail Recursion
    * d6 _# T* [; m$ R; T5 e
    9 o7 g5 i- Y. y9 g' p. LTail 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)." w0 _: W+ A; n3 |

    6 y, q% ~; P( E$ N  `+ VIn 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-14 00:01 , Processed in 0.030291 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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