设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 3 U- K( A# m/ h

    / ]+ P- M! A% H; }  C解释的不错4 U3 p/ [* F0 ^( |7 o. v) g) L. |

      O& c: {+ \( P; v/ O递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! @, {* R& \+ [8 [# r) u

    7 y) g2 |+ w6 X) ^0 Z& _7 K4 W 关键要素
    9 }- a0 o0 H1 J1. **基线条件(Base Case)**( |$ R% F, ~6 `- c
       - 递归终止的条件,防止无限循环( S# C. \7 I5 U4 f! O
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    3 f! v$ W( h, Q2 x/ f: F; E6 }8 Y3 a3 s
    2. **递归条件(Recursive Case)**5 h- w$ A  l9 ]$ l: f: Z
       - 将原问题分解为更小的子问题
    . _5 \' ~% e* h! x8 b   - 例如:n! = n × (n-1)!
    8 e1 ]; ?4 T" [5 p2 ]" [% o' I, K% k$ H4 ^' w5 I/ Q2 Q% `" w; k
    经典示例:计算阶乘4 x8 \4 D5 o* c6 @8 |  o6 A. N! N
    python  }; g' B5 f0 C8 F# `( n
    def factorial(n):
    : u; @' ?3 B/ E; I# \2 s    if n == 0:        # 基线条件
    9 ^: U4 I9 N- M' @2 K        return 1
    , u( k. s: ]7 |, o    else:             # 递归条件
    , m2 I0 l6 W1 i' ?2 a5 x$ R: X' a        return n * factorial(n-1)' i/ ~6 Q+ d8 s; T) s
    执行过程(以计算 3! 为例):- R: y2 M4 m) F# b  X3 X! ?
    factorial(3)
    " t! Y- i. O4 `- Y6 C( K4 D3 * factorial(2)
    6 [  l) I5 Z2 M2 o' Z8 k9 L9 r4 E2 w6 c) N3 * (2 * factorial(1))) M! S5 K! d, b/ @
    3 * (2 * (1 * factorial(0)))
      |! P7 K) d( o* D3 * (2 * (1 * 1)) = 6
    6 w4 S9 D- q, P# Q, A
    $ X% ]; L' f( r9 N 递归思维要点
    3 r4 x) T3 N2 f  s5 R; f* n1 H4 Z: M1. **信任递归**:假设子问题已经解决,专注当前层逻辑6 U/ r* r/ h+ z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    8 t, u  W' X  M1 f3 Z3 z/ e9 y2 X+ M3. **递推过程**:不断向下分解问题(递)
    , O- K/ u! |! E4 k4. **回溯过程**:组合子问题结果返回(归)
    8 J: g  ?; U2 P& K& Y9 @; `: ~: L7 o# G. T. k$ [# {2 H
    注意事项
    ; n. n: D5 V3 R" L必须要有终止条件6 |3 i! J% m& U6 M  M
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 K% @2 [* `% d, N: R0 E7 [6 F) c
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    : K. ]& k- ?+ y' |尾递归优化可以提升效率(但Python不支持)
    ' Q- J" b) A4 t. R
    4 Z/ e7 h& {9 m1 ` 递归 vs 迭代  {7 j3 U, v) b$ y+ f
    |          | 递归                          | 迭代               |
    3 G* ?  A9 R9 S4 C- i3 Q! @7 `$ h|----------|-----------------------------|------------------|  T$ b  _1 u+ X
    | 实现方式    | 函数自调用                        | 循环结构            |
    ! ^; R; H7 c; _7 O6 P% f| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |* Q# u7 a4 }  d2 C
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      g+ k% c9 _/ t6 {7 G) t| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) ]+ X# ^3 i! W6 C& b; c6 L
    " @, M% [! K1 C
    经典递归应用场景, }) {/ Q. ~# e7 d0 v" ~* U
    1. 文件系统遍历(目录树结构)8 B; K. U! y' ?4 d* Z. h6 K* m
    2. 快速排序/归并排序算法* l. ]. R; s9 `, ^* x6 z- e
    3. 汉诺塔问题
    3 C1 J1 G* l9 Z7 \1 [4. 二叉树遍历(前序/中序/后序)4 Q( j! Z. F" w
    5. 生成所有可能的组合(回溯算法)+ Y! q+ h- k9 Z, r2 s- h, D
    " c- d' k4 Z9 A5 z( U' y  q- a
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 09:43
  • 签到天数: 3167 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % U. k: y- j5 T6 e2 A我推理机的核心算法应该是二叉树遍历的变种。
    ( z4 k$ a+ V" y+ ~7 L! H  N另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . I  \' e6 ?0 q  I  T4 ?, c. VKey Idea of Recursion+ O" y  N  H' E, m: h
    ' w* z! _1 |# {4 p
    A recursive function solves a problem by:
    7 U5 v9 t+ c7 Q- {! X8 c
    ; y/ g, a& `* h, H: d- D( Y4 ~% V$ @    Breaking the problem into smaller instances of the same problem.
    , W. r6 \) ^+ ?/ W/ N5 }. z7 {$ Q5 M* @5 N# C1 l. D2 |7 ^2 P
        Solving the smallest instance directly (base case).( e6 n: O- ^% E0 H0 B5 b: l) w

    8 ]& y$ K; l5 l( U7 J1 X' X6 |    Combining the results of smaller instances to solve the larger problem.* s0 K9 r: [% Y( X/ r7 _5 ?! F1 L) r
    ' v; g  r8 n( r3 A
    Components of a Recursive Function6 K/ W. Y/ r9 Q7 f& d
    # ?7 d& w; ~3 h& o2 m
        Base Case:
    ' G9 V  d2 L' X- i% w
    + J6 z$ G8 h- S, s3 N. o. P        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.3 ]" Q4 R7 u/ q3 K! R+ C

    2 w) N/ U/ E* \' K6 r        It acts as the stopping condition to prevent infinite recursion.+ K$ c3 E2 }% |+ a  h

    3 y  r8 w6 E( m        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.; W3 t5 H8 }& R! R: S

    1 p6 r; r5 k0 \6 [( _, T7 c. t' U    Recursive Case:( m6 |/ @7 Q- m6 f6 Z3 l

    . A0 w: @. J& V        This is where the function calls itself with a smaller or simpler version of the problem.: q3 Y+ W) e( w

    8 x& p3 z- c9 r; ^1 b) C' r        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ' ~7 I# R  n+ E- x2 O+ [- h4 [) G  z; y7 a0 F" w* X/ E
    Example: Factorial Calculation
    % X9 V5 C  K3 o5 i$ {0 @1 J
    $ [# ^, D! Z% X8 X) BThe 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:
    0 w$ x8 G% J1 A( K6 q8 ~' [0 a7 R7 `; H1 Y5 h" Z, q5 y
        Base case: 0! = 1
    5 U; ]: K8 H. [, d& ~% c, u) K! n, P6 i
        Recursive case: n! = n * (n-1)!1 W' F+ m* H& m; j8 [. H6 R3 C

    7 n0 c( w5 t5 e1 S- ?7 OHere’s how it looks in code (Python):' X. N) n( d% f
    python
    , R6 S, R' v3 @
    ; q0 i+ L; G% Q/ i( K' B+ e' W/ \) y& c, H0 d  m' _
    def factorial(n):/ z& S+ l9 k5 Y
        # Base case3 @* `+ M; [/ c1 F; e
        if n == 0:5 e- S5 J5 E0 d8 n; w
            return 1
    $ l" x0 O- X0 }" O    # Recursive case9 a. H  d/ N# T2 g; O
        else:
    $ Q' o( I. P) M' F2 L/ n+ X        return n * factorial(n - 1)3 f5 q/ U6 A: T; ?4 b. W7 S& _/ t
    0 Y! _- U1 B3 u: d8 X
    # Example usage
    ' y3 \0 z* _" `1 I! n. S5 Tprint(factorial(5))  # Output: 1206 a6 D- W3 f. J5 Q6 V' t0 D

    2 @) q+ `+ `0 p2 z7 PHow Recursion Works$ E( b2 B# o% U0 ~( @% n# g, W8 Y
    9 z8 c% q# T7 k
        The function keeps calling itself with smaller inputs until it reaches the base case.1 n8 K4 I% u3 n
    % U, i$ V" |0 Z
        Once the base case is reached, the function starts returning values back up the call stack.
    ( c! ~- j8 J6 h8 h' A: I7 ^- F- ]8 F1 o. S5 N( v, `& M
        These returned values are combined to produce the final result.6 z! ^! B# I) R' e/ b; z4 @0 @
      U- r' h& g/ u7 `* S  F
    For factorial(5):  J6 V9 J2 L( R3 ~* Q

    ; g+ C) ^; ]4 ?  S0 g7 j( S" v5 m
    factorial(5) = 5 * factorial(4)
    9 i' x6 i" [2 ^) hfactorial(4) = 4 * factorial(3)' Z( G; y2 ^5 h' d# r% r
    factorial(3) = 3 * factorial(2)
    7 c6 ]4 U5 Q+ A2 ifactorial(2) = 2 * factorial(1)( z+ z2 B3 h/ E$ h# ]" C- Y5 M4 I& X/ _
    factorial(1) = 1 * factorial(0)) Z  k$ i$ J$ I( C
    factorial(0) = 1  # Base case8 z! l. s9 f# ?. U! T4 E
    8 E  N% |5 P7 H8 O" C! e
    Then, the results are combined:7 k5 _( x: F" U: g: f
    2 c3 e* t5 }& b) w

    + Q: u0 W5 ?: Dfactorial(1) = 1 * 1 = 1! X5 b5 }4 f- A% r, @; Z
    factorial(2) = 2 * 1 = 2
    ( {2 @- h1 z" [, q2 |+ efactorial(3) = 3 * 2 = 6
    & A& _1 b9 T" ?2 S: C6 \factorial(4) = 4 * 6 = 24  t& h7 ?1 a$ w4 i: w9 a$ S
    factorial(5) = 5 * 24 = 120
    & u7 f7 @& L' ?! L/ c0 n
    % a( }2 [0 @6 v$ vAdvantages of Recursion
    ( t! W  X, Q$ O. W8 M. w% s* U# C
    # @% [2 w7 U; V9 E    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).4 b- z! O! B3 X  A

    9 q6 `3 J0 j2 r+ e& u    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    1 p. h: m" V' e* F9 ?
    ( u: T! g1 s% z2 fDisadvantages of Recursion
    ! M4 I% E1 f: y6 ^- _' E# c3 x1 _. g( B* i
        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.  J1 d6 r" M7 N5 a
    - v8 F& Q  Q1 j7 Y+ d
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    , B: `" u; H9 q8 V4 y1 S( B( L: P- w
    When to Use Recursion
    % s- J( E( O* J3 F3 O$ K. V8 t9 f! k% a0 `
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 T! R) c/ N6 V, |8 M6 ]2 [! ~9 c) G) x, `& r
        Problems with a clear base case and recursive case.
    ! R* Y  I9 m/ n6 Y
    1 S; g, H% ?2 P, T$ w& D* x$ M4 SExample: Fibonacci Sequence4 }5 }+ }1 D0 [+ P/ T

    % `8 P$ s, ]; n8 y% e" j2 sThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# ?- z$ t' R1 f& i$ d' ~5 f: }4 L
    - d' f  ]& J4 Z+ h0 E3 M
        Base case: fib(0) = 0, fib(1) = 16 V1 a  j; C/ b/ Q9 U. Q0 r6 [
    4 o7 U2 |' ~  e: e+ l
        Recursive case: fib(n) = fib(n-1) + fib(n-2)# ~& n+ {8 w1 Y; X+ B/ a

    7 H; j4 |) C- T+ N% U9 zpython! v- N2 F' f4 \( K! G7 o" r7 u

    $ F4 I9 f# q/ a8 B3 p( O0 O1 j- ^' l' _& j; b
    def fibonacci(n):
    * I: \1 c) ?9 e+ F9 s0 H    # Base cases- M! i9 _6 s  J5 U' t
        if n == 0:
    3 z* S! ]/ r% \/ U# l  x        return 0, m) T4 Q2 k; u' d  B, w
        elif n == 1:
    8 u; @: K5 ?# X3 P! p: Y        return 1; C. |: a' s# [' c- i
        # Recursive case) d3 k3 M& b2 T! ?
        else:
    0 Y1 g* x6 C3 F7 f+ E( U  s  N* l        return fibonacci(n - 1) + fibonacci(n - 2)& _) y4 [& F7 P3 P

    $ v# o. C. q8 ?  `# Example usage4 c9 I) k- Q/ a
    print(fibonacci(6))  # Output: 85 g$ K2 F( m# c5 }4 a. ~
    $ r: R' I8 q/ [5 {3 F+ f/ |! @
    Tail Recursion* V3 {) w( ]: m0 E5 X7 b& z( P0 D

    2 u$ E2 H7 i2 f' }9 XTail 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).
    1 h- D5 H( X! d5 p3 K3 }3 ]  j- H: F4 f  q1 R: E+ F
    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, 2026-2-9 03:06 , Processed in 0.054303 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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