设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , y) I( E& {2 p7 @1 y
    ! E  n' m9 |, t& Z
    解释的不错
    4 c- S1 m, c" H0 C8 r3 t! ^$ y0 p) r$ e! D' E
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. u" Z0 |) q, {* i- L( h: q( L

    - q+ D& e8 Y" W+ Q& [& C4 {8 x6 ~9 y 关键要素
    : C! ]1 I. ?) F  P& L& }- r1. **基线条件(Base Case)**4 g, j& C# ?# L8 d% w
       - 递归终止的条件,防止无限循环
    ' d4 \/ g1 V3 R2 t: ~. K' |. B   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    8 T) e1 P% [8 {& l4 S
    . `; a7 s  M( h8 G5 y: h2. **递归条件(Recursive Case)**
    ( }$ ]: G1 T4 _" l* P$ o" D   - 将原问题分解为更小的子问题( |  |6 @7 G  P4 B2 K
       - 例如:n! = n × (n-1)!
    2 _+ t0 `7 h4 i4 A' }; A( ^, k$ l( U  @: L$ t$ f( v/ v
    经典示例:计算阶乘  `! l6 ^) ?. X/ }! c* ^$ c
    python
    8 t2 r, q# Q4 w! Xdef factorial(n):. L" K  [; R$ G& l
        if n == 0:        # 基线条件$ T# e4 J* M. A+ U! k# ~
            return 1
    5 M. p; [% o: O. R# q    else:             # 递归条件
    5 R3 p7 z/ {) n  D6 V& y        return n * factorial(n-1)
    - I) C" h: f" r$ t  W! b  l" x执行过程(以计算 3! 为例):0 B/ H9 v) h" `2 x0 m( U! n! e+ K
    factorial(3)
    7 E+ X3 ]/ j6 U2 A3 * factorial(2)
    ! o2 Y' v) s7 f9 u# s5 B3 * (2 * factorial(1))
    6 }, p7 e- g5 V$ o" z  e3 * (2 * (1 * factorial(0)))
    0 X6 g6 j" w+ D3 K# H8 {  O5 ^3 * (2 * (1 * 1)) = 6
    $ U7 k+ X+ l. e+ x+ h" v6 N$ D, i& H: i7 ]
    递归思维要点0 v5 s; v& u* x( K" K, p
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑- W$ ^2 [' P$ B1 |8 a# J( d
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 N+ i; |7 B2 v3 V/ z4 a: m3. **递推过程**:不断向下分解问题(递)+ P  b# ~  h% h
    4. **回溯过程**:组合子问题结果返回(归)+ \& A6 A: U* ^; n8 A

    / ^: |) {$ Q1 S' Q; f( [) b1 ]6 R 注意事项
    ; E: ^  K, g; Q. }8 d/ F, i! E. R6 ?必须要有终止条件
    * d: `3 ~6 p* f1 R4 H* j递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ q4 N' z3 B# J0 g+ H5 S
    某些问题用递归更直观(如树遍历),但效率可能不如迭代" S% T3 v+ m! f' @  i$ r
    尾递归优化可以提升效率(但Python不支持)
    - y+ Z% r5 \* B) B& D/ T/ C4 `; \& B  T2 l. z7 o
    递归 vs 迭代6 ^8 N7 `8 {" E! f$ y' w. X+ _
    |          | 递归                          | 迭代               |3 I  c) {8 x1 J
    |----------|-----------------------------|------------------|
    " k" j4 E2 R! m; S| 实现方式    | 函数自调用                        | 循环结构            |# L6 y7 I/ o( i' H7 [) w
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ C1 s7 ]* D8 z( c
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! i6 K" R& A9 B, ?| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # P2 E! e- w, g2 m3 v' n/ }- v( x. Z8 g# _9 h
    经典递归应用场景
    4 M( L5 M" S8 a2 g! B1. 文件系统遍历(目录树结构)* V, f8 c9 ?& z1 M- ?' ^
    2. 快速排序/归并排序算法
    4 e9 J: j8 E* z! Z8 J3 c0 Q3. 汉诺塔问题
    4 B) U6 [- h- N5 K6 C4. 二叉树遍历(前序/中序/后序)0 j4 U7 X2 e& e: M& H
    5. 生成所有可能的组合(回溯算法)
    ) y9 o) E0 I: {; P
    " [3 N+ _, F' K& W; n! [试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    11 小时前
  • 签到天数: 3113 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,! W+ R9 _& x$ N, L
    我推理机的核心算法应该是二叉树遍历的变种。# t9 o: A4 N3 S1 a
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 Z" `" f8 N6 k" CKey Idea of Recursion
    ( y8 g$ K6 L! }' U. t
    8 m2 a$ Z2 h0 L5 ZA recursive function solves a problem by:, j0 x9 v$ {  D8 ]" B3 O. z: C, ]

    8 L; E! J0 R6 W: d0 S. O    Breaking the problem into smaller instances of the same problem./ @1 N5 W0 l# |9 v& Y4 i1 P& Q- n

    2 T' g8 }0 [( ^) a' d) b    Solving the smallest instance directly (base case).
    ( X! G) A' B5 V- S& y% ]$ k* I1 ~/ T! M
        Combining the results of smaller instances to solve the larger problem.9 n  K. m4 E2 t, ?- E! M3 F
    ) b( I: J! y! I/ d. f
    Components of a Recursive Function, F9 X. g  c& m3 {/ b* K6 Y5 D+ O

    ) D$ B4 J) z- b/ w: T  G; w1 g) `    Base Case:4 i, {+ A; z' y* n1 |

    2 j" d) W8 d1 Q" O, _# @" F* T) r        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* A- [  t/ d- q! K# l/ E6 n, P
    ; ^7 |3 R  m9 m6 s
            It acts as the stopping condition to prevent infinite recursion.2 F/ q2 X7 G' O& S" z( O

    ( _: y2 ^( D: y. \        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ! R# X7 D* [9 V1 r, k' q4 ^  X7 r8 |  R
        Recursive Case:
    8 ]# z' }. [6 W. [% F( e% |6 Y+ m. h( ^1 T5 H; D1 j7 h
            This is where the function calls itself with a smaller or simpler version of the problem.% d6 h* i! [' z$ X7 M" P( R
    ) ~3 z7 h( q9 ^" p6 ^" S* j8 X( F
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).4 I1 P! v+ u1 L  Q

      N- ]- z" O  V. V/ n' j, ~3 ~* wExample: Factorial Calculation( _/ C8 w0 o+ [% u" ]. T. Y1 r! J

    / o2 C4 B) N( b$ fThe 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:
    6 j4 q/ y3 G' l4 O  b5 H, a! ]. g- H% N# ]7 _+ i9 p
        Base case: 0! = 1
    / ^* `; n& M* }$ {* {* F$ i3 j2 a1 v' g
        Recursive case: n! = n * (n-1)!
    7 H7 X' H) X  R9 ~% x) N$ R" t- `# o9 J- h. f0 Q
    Here’s how it looks in code (Python):
    ! W- \6 K' H( v; @- K: Xpython+ g0 |! p/ j) a/ R( n3 _9 N

      t# ~& z" c( G' B+ E9 ]. o2 x0 L5 A
    ' c" i0 c# m1 V8 ]def factorial(n):
    ! d1 \- s( d, Z6 Z4 J7 N    # Base case
    $ R  w! _- a' ~3 E    if n == 0:0 W2 i" X1 X4 H3 I1 `9 [' X- E* g
            return 1
    3 c0 p% w, m" J    # Recursive case1 q7 [) ^, l! g- V; }
        else:$ ^6 V- C5 W* u, W7 n
            return n * factorial(n - 1)
    ( h! [2 }. F5 A& M, K; S- H/ \; K7 F& g) l7 `0 E  {
    # Example usage, q$ Y4 D3 ]. H8 T- y9 U
    print(factorial(5))  # Output: 120  Z/ e1 n, V% m6 |1 T

    9 L  }* O/ W9 |How Recursion Works( n$ P% B/ h: a1 }4 p, N6 V+ X

    9 q6 V9 {! J: ]    The function keeps calling itself with smaller inputs until it reaches the base case.8 {8 W" N! d, Z3 A
    : ~" f8 Y( I) T9 o" F* a
        Once the base case is reached, the function starts returning values back up the call stack.
    ) U2 x- {% x* m9 O- G& Q8 N' m" f9 L- q- Y7 M9 Z
        These returned values are combined to produce the final result.
    4 Q* n' I, b8 r9 Y
    3 w% ^. R* K( r2 |, v" RFor factorial(5):  Z  r& @" A* T6 i2 u- |

    + }& s, n5 I7 {( @) B  X
    5 G( S- D4 I5 l- `) \factorial(5) = 5 * factorial(4)0 p5 M3 M5 A$ S0 O. g) a
    factorial(4) = 4 * factorial(3)
    ( a+ j8 V9 K% m0 c9 G/ [* hfactorial(3) = 3 * factorial(2)& |5 u: f% t4 U# J/ E& W
    factorial(2) = 2 * factorial(1)
    - C7 c8 A  b( c# W( L: p) Hfactorial(1) = 1 * factorial(0)( ^% c' q7 o- g1 W' Q$ u
    factorial(0) = 1  # Base case
    1 I' o1 E( \2 g# O6 P# W" w1 W% J9 b' t+ x8 q
    Then, the results are combined:
    : A8 u& B7 j. G0 Z6 y" W1 l# e3 j  v' @

    " `  U" e, e4 k4 X; J# D% ^factorial(1) = 1 * 1 = 1
    $ @" h# v# T  m/ ^9 U+ N7 lfactorial(2) = 2 * 1 = 2
    ' L" f& k4 k3 \3 [9 Bfactorial(3) = 3 * 2 = 6+ N( M1 F& Q2 O" T7 n! n
    factorial(4) = 4 * 6 = 24. W/ p; L5 C" A6 Q( Y
    factorial(5) = 5 * 24 = 120# e1 Q; M8 g2 ^2 f3 l' l

    2 l) _- t6 D, ?' B+ F  W2 H8 GAdvantages of Recursion
    4 y5 S$ }# H( R7 k" c8 t" q3 _, X' s5 f1 D# m1 k- {8 U, B/ G
        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).& z2 B5 n4 d$ h- @9 k0 w

    " @2 p0 K- X1 I; `$ h    Readability: Recursive code can be more readable and concise compared to iterative solutions.: {. M" o. I5 E( y4 \

    8 r% D# T: E9 t. S& FDisadvantages of Recursion' g6 n# B3 x  D7 M( o- G( r/ t

    - h/ a5 Z4 t$ |  ~1 X* {# R7 y* R    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.+ N0 O, e, z% G% k

    1 j- C/ l7 x4 O! s    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 q+ W3 M+ }" Q( l- \+ c

    - ]  v: h% f0 a# iWhen to Use Recursion- u- I' Y1 A2 Q1 \! v
    , f2 C! a. v* D5 L5 S8 H6 E
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    6 d! M, g+ @* E
    " U( ^' @7 |1 h    Problems with a clear base case and recursive case." k; [9 w4 B- y* ]( @
    . ^. Q) K+ c) P" F0 U; n; \
    Example: Fibonacci Sequence
    . I7 \) {! z  x6 K% c8 q
    ( y0 {5 c/ \% j% vThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 {% o3 j$ l% R! r* [/ L- ^5 Y
    1 f( q; J1 r6 B1 _0 S, b. n0 S' P
        Base case: fib(0) = 0, fib(1) = 1
    & }- r. @. \) i4 M6 j1 @! y
    . f" f5 f( b! Q  ~- c" G4 F5 {    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    % K( U1 i, |: C. X0 A5 H; B4 c& A; W2 A1 m/ m9 W1 Y
    python- ^/ [; s# ]1 ]6 p' p* y% f

    7 [5 S/ `) {7 C" J6 S/ y8 y
    & k5 {! ~0 J$ N7 l, t8 P0 X& ydef fibonacci(n):3 D! _+ w, D; M- Q8 {
        # Base cases$ r+ V. u. K0 w- Q5 S( M, Y
        if n == 0:2 ~+ w) e3 R/ f
            return 0/ w' \$ R$ A4 A0 y
        elif n == 1:
    9 p( W. F6 M5 c, b        return 1
    - u0 V% M! ]8 G1 n: a0 n5 h( F    # Recursive case; _6 Q, p2 S7 S
        else:4 r3 h# u( t4 y7 a" I7 v( u
            return fibonacci(n - 1) + fibonacci(n - 2)
    / F( x) u. J' ~% V& {4 D% K- ?
    " f0 |- n; V4 N5 x/ y6 p# Example usage
    2 c& i" D; d& ?! J+ mprint(fibonacci(6))  # Output: 81 f- f5 [7 y& Z  j, f
    ) N' K8 Z) v! Q5 _6 F! }- n3 D
    Tail Recursion! }. y2 V: n" M& V( W  l
    5 ?0 A; V; G: S: `3 n
    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).
    " X% W+ d+ R4 f" y4 C; `
    / M* U, B) q, W. o3 RIn 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-9 18:45 , Processed in 0.041920 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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