设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 a( r8 o( \" d# F9 p0 N
    ) ~+ K' M6 Y! A: y, k, ]解释的不错
    : M& ?( y2 P( A" `
    / g* O1 ]. |5 P% G2 w7 a9 m6 |+ J递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ) q$ v" B9 W4 C: F; I' k  r, s. u. o+ F8 m# Z8 D0 v3 W
    关键要素/ Y) {8 z5 T! b7 E" s) L
    1. **基线条件(Base Case)**
    ; E6 a( l8 ~- p" Q6 H' e# l9 w   - 递归终止的条件,防止无限循环$ g+ y$ r, \) g' _2 P8 q
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1" o* w! z! U" M4 ?2 o. a

    ; }% v( k6 |9 j5 |0 Z' R' R! ?2. **递归条件(Recursive Case)**
    7 X  T3 u- R6 s* [; s! s   - 将原问题分解为更小的子问题% j& v1 I0 e: t
       - 例如:n! = n × (n-1)!
    * q# V0 F& ~/ Z2 w! l0 M5 L) l' C
    ; L& g4 s+ G, H* [) \3 [ 经典示例:计算阶乘) s% a& W8 F% P  s
    python0 N8 e: g. A) `' a. b
    def factorial(n):1 z2 c" q! m8 b9 {/ ]
        if n == 0:        # 基线条件
    * }8 Z2 m* K" x9 c' M  ]$ O1 W        return 1
    6 P* q! a) E7 M    else:             # 递归条件; N! W1 G) h: |) Z
            return n * factorial(n-1)7 {+ o6 B$ ], b7 _8 F, M5 [) \
    执行过程(以计算 3! 为例):
    % x3 L. A; n* f% T$ C. Cfactorial(3)* `. q% O5 |! N
    3 * factorial(2)! h8 h$ @- K  m" \5 a% C
    3 * (2 * factorial(1))8 v4 u$ W* `- I6 I% m
    3 * (2 * (1 * factorial(0)))$ u2 K% c2 K2 {' g3 o5 Q
    3 * (2 * (1 * 1)) = 6
    4 y2 o! ]  [) H+ U: p! U9 g
    5 ^, b7 z. d) c5 S% m 递归思维要点- ^/ g9 t" o; {& ~& x0 k
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑3 @7 K& u6 w/ v
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)8 _6 w- p. Z7 H, H
    3. **递推过程**:不断向下分解问题(递)+ ?$ W/ ^# b, E7 e
    4. **回溯过程**:组合子问题结果返回(归)
    : j: u7 }8 i: z
    + }- j# l2 b$ e% \ 注意事项4 l6 N+ V* X5 s/ o  k& I
    必须要有终止条件
    6 s' h0 ~" T8 B$ T7 a递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    # y1 k! N8 t# A- k某些问题用递归更直观(如树遍历),但效率可能不如迭代: R3 [" z! P; U1 R
    尾递归优化可以提升效率(但Python不支持)) R2 ~$ q% t7 C1 _! t

    ) P- B, L4 K) f* ~ 递归 vs 迭代: Q+ o+ w' h) v  H+ {
    |          | 递归                          | 迭代               |
    3 h  m3 G" l7 y$ i! ~! d|----------|-----------------------------|------------------|/ g2 A6 n# h+ {) d5 l
    | 实现方式    | 函数自调用                        | 循环结构            |6 P& L4 F( X9 b- A/ t  g+ C, Q/ N" o
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |# W7 Y6 K* r# h
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |( [5 N, K3 c) ]
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |; D; C4 F+ a; `  I5 u. {7 u. K

    7 P# L* d; h! S' O+ J 经典递归应用场景3 i  B. A3 T' @( Y; H# k* u: I
    1. 文件系统遍历(目录树结构)
    9 \  D3 C$ W- Z: J* n2. 快速排序/归并排序算法
    5 G6 h6 H; w- f: d- {! K+ P! x3. 汉诺塔问题8 T* `5 E$ K; ]( U; e: M7 \5 o8 n2 }
    4. 二叉树遍历(前序/中序/后序)
    9 X) n7 W" I8 ~) {5. 生成所有可能的组合(回溯算法). u2 j& b. Y! r6 b
    # }, Q2 N" ?" `; ?
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 05:46
  • 签到天数: 3238 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    / \" H) \, M; ?% T0 H我推理机的核心算法应该是二叉树遍历的变种。. m) A  U3 ?" _) v" P5 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:: b4 a% F# j" }- f1 d, X
    Key Idea of Recursion
    3 o/ l; N& X+ m# ?. I
    ; B7 @) n/ K/ t% g  |) A) UA recursive function solves a problem by:- w& n; `- u/ v1 L& X5 n5 Q& B  i5 K
    , E0 j( ^" X8 [2 |
        Breaking the problem into smaller instances of the same problem.1 E9 D6 G/ W) C4 F  E
    , P7 r: n! l3 f$ ^6 [7 ]# `; m* n# ^
        Solving the smallest instance directly (base case).
    5 p  V8 e+ r# w6 u* `+ r' {( K' s- m  a- p1 M
        Combining the results of smaller instances to solve the larger problem.
    / J% f1 v- l3 t0 E& P5 o2 z2 S* ~4 b( p  c& l
    Components of a Recursive Function9 ]5 Y4 _( j/ x2 Z+ ^1 B' F
    ' {/ k; A9 s  J9 \/ A6 G
        Base Case:5 K, k6 m: q, }$ V
    & A* g- X  ^8 o7 z/ C: U, o$ J  f; j
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    * R+ Y1 W* j" B" d$ z$ V
    $ j9 r* U8 Z! U* Y$ B# m3 i1 Z9 u        It acts as the stopping condition to prevent infinite recursion.
    + |" U8 `2 }4 C, s: ]6 T" p! x/ D. }' r. c5 G1 f  \% O
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; q% g( K! W' h7 w/ X: c; h
    % |0 S( U( S" k6 z1 w5 Q! _    Recursive Case:
    7 S5 n& }" k! ^" A6 u. U- U
    1 V( W3 D5 X8 ~  @( s/ _6 E8 W) q        This is where the function calls itself with a smaller or simpler version of the problem.) e! n" W' G: G, l8 _

    7 V" [* a& R5 i0 K" p1 k9 z  U" `        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 e4 I, N- j; H) I! G6 B, o
    9 _+ c3 [+ H& [. lExample: Factorial Calculation( w; f5 ]/ a9 R0 |" f

    " p) Y% h2 a8 S% tThe 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:
    $ R3 b. e/ ?# l9 z
    9 `' h  y' m$ m! g4 v5 V/ H    Base case: 0! = 14 Y1 s% C: y9 a: r; a

    . H8 e3 h. `' R$ W' m7 C    Recursive case: n! = n * (n-1)!
    ; q' Y4 B7 f' ~+ V) O% ^+ o8 l" m9 }( o$ y8 i" W/ C6 V7 j  W
    Here’s how it looks in code (Python):
    * G8 \5 P1 |+ y7 a: ypython
    1 P2 E' H) w, _
    0 d" E8 I. Y# k( y1 }- x0 E1 H3 S( w8 V, g& d( q* L) s! C
    def factorial(n):" U+ d0 E: r4 a  R
        # Base case
    8 {' ]! R6 n9 O    if n == 0:
    $ ?  C- ^/ t  M* y; D        return 1% N1 o" `  ]# Y
        # Recursive case; l. {6 f- K$ h1 p8 ^- W( w6 _0 b
        else:
    0 i8 r8 L4 o1 E0 V3 p: f: l; E+ h        return n * factorial(n - 1)- L6 K1 V& i/ _+ A2 V& B6 T" D5 c

    ! q; C6 n# q9 j* ]- t# Example usage5 ?0 ^+ o1 H* v5 E- z
    print(factorial(5))  # Output: 120
    ' v* _0 S5 U( F, m4 s
    , i# z" ?) n1 PHow Recursion Works
    6 H9 x% W0 v! y+ c
    . e- F/ Q% G/ l6 U% b) }    The function keeps calling itself with smaller inputs until it reaches the base case.
    9 O  _# V7 _6 P6 E8 Y
    0 F2 l* h7 N2 U0 }0 {    Once the base case is reached, the function starts returning values back up the call stack.
    , i# V* D& s6 h4 Y0 Y6 v% m1 [/ L1 q
        These returned values are combined to produce the final result.# L8 ~7 _  @& L( I
    5 r+ h# h# ~2 w3 k1 `( k5 t/ w
    For factorial(5):
    # b8 U- C' \: z) ?9 c+ E$ N; I0 ^1 Z+ v* F$ t% b% d) ^
    / E7 O. u6 B# c6 I
    factorial(5) = 5 * factorial(4): Z3 r! X8 Z6 I! P8 r
    factorial(4) = 4 * factorial(3)
      `: V" O" g1 p( I$ q; ?factorial(3) = 3 * factorial(2)
    # P# O& r  l0 R3 i) Qfactorial(2) = 2 * factorial(1)5 t# g: }6 A$ o8 F& ]) R; C
    factorial(1) = 1 * factorial(0)
    - g- p, V2 U/ x- Q# Tfactorial(0) = 1  # Base case' y* v/ M& k* x" ?' o

    5 M; A2 L/ [) }- Q& r5 ]Then, the results are combined:
    2 y2 Q2 Q7 H! [. n+ J4 I' n# B; H
    / e; J: e5 X& M  B6 K8 j0 L1 V8 u! W
    % y7 @/ v6 ]. t( Mfactorial(1) = 1 * 1 = 1; h; o0 S/ }, }' a! i
    factorial(2) = 2 * 1 = 2" [3 u. K/ n& G- Q/ e
    factorial(3) = 3 * 2 = 6" ~, f' a" T" M
    factorial(4) = 4 * 6 = 24- m0 J& p' X# O- l
    factorial(5) = 5 * 24 = 120
    # B6 z& F; ]( U* l/ h4 Y
    7 S! T) s1 g  t4 \7 l( I. VAdvantages of Recursion
    9 W8 r) O- V: u
    ' j* b( {% \# }$ }) i+ u, 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).9 f2 ]9 U; C+ \, q
      I+ w! ]& e$ C3 L3 r8 n9 T
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , V% U; [' J0 b1 ^
    0 @6 t& d1 t6 P4 wDisadvantages of Recursion' g. E* w6 e  M
    2 J5 F/ k# B+ s3 E- b2 p6 W
        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.
    ! u" |$ r$ G* F6 N, I/ d& Y& o( r/ u1 b& y
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 Y' m( W; K1 Z5 m
    8 Q1 B/ f5 E7 R( {1 ^& tWhen to Use Recursion
    & U  N2 Z8 o$ A/ S; J
    9 x4 k0 j* C! m    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 |9 f# U) L5 \& W; q% v

    ( h# w0 E: K7 ~' \6 E  a    Problems with a clear base case and recursive case.
    , Q4 I2 N$ x/ _# D6 z6 e+ p) b- J) _
    Example: Fibonacci Sequence* W. T6 E# G/ [' }! K

      ]8 I- @9 t/ s6 O' C0 mThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
      a% k8 }. e1 K) C3 F# \8 m# O2 T1 V, n$ [, S
        Base case: fib(0) = 0, fib(1) = 1
    4 V# u$ S4 ~# T& v, U# c
    ' {2 x4 z+ B9 V! u6 ^( ^7 o    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      F2 c/ a( @4 Z/ a7 C; A7 f+ n3 Y, i' m4 p9 P) _
    python& `' h8 z7 {/ t, L

    1 V6 g6 ?/ l. q' T+ z
    6 J8 J6 e6 n) T% N1 }def fibonacci(n):
    ' W* [( [7 `8 R% q- L+ O    # Base cases8 `! _/ a4 v7 ?7 }# d7 J
        if n == 0:# P3 W6 Q& Z; b! d9 s3 b0 H  y
            return 0
    1 }" B7 Y" ?8 U    elif n == 1:
    7 O8 D% B" P8 V0 a) y% W% A# }  L        return 1: u& @1 A) k+ u) K0 v
        # Recursive case
    - U/ g. ]. i+ j1 E! A4 D6 R    else:
    ! ?- v) l5 O7 B$ O0 q# T        return fibonacci(n - 1) + fibonacci(n - 2)% h" J1 ]$ T/ T4 C7 s8 Q
    " @2 d- J' W. d- h; ]
    # Example usage: I7 ~' B0 ?2 @" X( r3 L4 M
    print(fibonacci(6))  # Output: 87 K; s, M3 M& ^1 {3 `2 c3 P
    1 W6 P5 I4 q. ]0 X- {- e
    Tail Recursion
    1 [# e+ ~+ ]3 ~  A7 d7 d4 B# T" O- S$ E/ C; V
    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).2 L7 ?& T( P7 u; ^3 i4 s

    ) ?6 @/ [: O. f+ yIn 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-5-13 05:14 , Processed in 0.072349 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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