设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      I, W/ S5 i! K7 e2 o
      Q: V, O; \2 I& B) G解释的不错
    / X9 R% A6 i7 R9 y$ t) ~& _0 K" Y$ E1 @: Q/ H, y* J
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。" d, L+ g3 U) D8 [( t6 H) ?! F2 i

    , t" M" q& b& L- l- n: D& _ 关键要素5 X  W, Z: ]# Z, k
    1. **基线条件(Base Case)**
    9 y& r. f$ i6 B" r2 p9 D) K9 ^& T   - 递归终止的条件,防止无限循环- R" f9 d5 m, Q6 t0 y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 10 X' |& p3 V0 W! I0 E% G, b7 J
    1 v& t9 S5 v1 I) z4 B7 ]
    2. **递归条件(Recursive Case)**
    3 |9 X9 h& }( G$ `+ k( _6 N   - 将原问题分解为更小的子问题
    . V- P& l- L6 [: Y   - 例如:n! = n × (n-1)!6 p2 U9 B7 j5 l; N( X$ t! U

    * C8 V' }6 N3 R) L0 _ 经典示例:计算阶乘
    : K, z. D; ~& f! Upython
    1 n: C# e9 i) W0 c2 ^. ^( ?, Y6 `def factorial(n):! r7 r4 j  [8 F: j
        if n == 0:        # 基线条件- O3 G7 d+ t/ c8 ~: b0 W1 c4 N. i
            return 1$ v' D4 [9 L' M  ?  R
        else:             # 递归条件
    . ~; a- k4 ^  K, ?! c2 Y        return n * factorial(n-1); I$ }9 Y: |2 ~' ^* r3 v
    执行过程(以计算 3! 为例):
      f/ q) q3 C" @2 ^9 ]factorial(3): y6 Q5 k1 k( D3 b% C
    3 * factorial(2)8 j  C- Z; s- S7 g9 g
    3 * (2 * factorial(1))
    & J2 |1 Q2 p2 f, g; b6 V- }3 * (2 * (1 * factorial(0)))
    % [4 u# Z+ R" a3 * (2 * (1 * 1)) = 6# P! d- j1 S) a  R' P, n" G2 W
    0 c3 R% [, r( i9 z1 p( D0 o' o
    递归思维要点
    6 _, a+ W! S! d+ R3 |. L# y1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & M/ q( O+ J2 v% H' C2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    & m; o6 J- i  M; v; L8 j' M$ C3. **递推过程**:不断向下分解问题(递)
    : j  l  }* z$ l# q5 j& l4. **回溯过程**:组合子问题结果返回(归)
    ( X* d- T" p5 e5 x& ^' W; W1 D  z# Y% O, f  Y# U% l0 S
    注意事项
    - i* I* l" r: m4 ^7 ~+ a& e5 Q( x必须要有终止条件
    . J7 a* R' {+ F- Q递归深度过大可能导致栈溢出(Python默认递归深度约1000层)5 y# a, H+ F  v, z( w. d
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    9 p0 \" G6 t/ l& J) P尾递归优化可以提升效率(但Python不支持)
    * b1 k3 I$ s6 h& J* [' `, Q5 \' \" [7 g. i3 o! k
    递归 vs 迭代) c9 h5 A% u# Z
    |          | 递归                          | 迭代               |. M- @" W* N: J7 P. n, q
    |----------|-----------------------------|------------------|8 O  v5 a" e+ @
    | 实现方式    | 函数自调用                        | 循环结构            |
    # N, V9 C, X: g| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! g/ `$ S. }/ w1 S( M9 s: ?| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    , C6 [+ T, o& Z* X) || 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. u/ q4 z) y- T: b/ s
    # w3 b& ]2 [4 B
    经典递归应用场景1 q7 |3 T% {; R& u
    1. 文件系统遍历(目录树结构)
    ' K7 c' A  U* u  p# ?! k2. 快速排序/归并排序算法
    ! s+ y/ G& k* @4 _3 Q$ ]" r6 Z1 t: m, O3. 汉诺塔问题* t: Z8 c  \' H3 F" F" t
    4. 二叉树遍历(前序/中序/后序)
    : S- W/ @% c9 Z7 q5. 生成所有可能的组合(回溯算法)
    / r' P1 ~1 m8 N2 `
    + h3 V) [/ W8 L试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    13 小时前
  • 签到天数: 3181 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,* q! V. m% v; \1 z
    我推理机的核心算法应该是二叉树遍历的变种。6 Q1 Y9 h0 v+ h0 {) J
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 p6 e) {/ s3 [# n, x% YKey Idea of Recursion+ L: L% O% @! F, C2 ?* b
    1 R/ O5 `- I! y- D
    A recursive function solves a problem by:
    2 X5 q+ `  N; y6 L. s- f0 A1 Q( g4 ~$ _% O* K
        Breaking the problem into smaller instances of the same problem.$ _6 E! f  L: j! @

    ' ^, g* V4 {4 S* u0 d, S# m    Solving the smallest instance directly (base case)., M5 s" E' l/ y& e8 w

    7 R& C. s. Y, K. I; O2 X* F    Combining the results of smaller instances to solve the larger problem.% D4 _3 V4 P  K( d4 \
    * V5 B: x0 {4 z+ e0 H1 c
    Components of a Recursive Function
    5 y3 i) a  w9 H( ~7 U* E4 H1 A: i9 r& F/ o6 i
        Base Case:- j. w# E8 Y3 S3 L+ F2 y

    1 G1 L9 Q# v+ r8 B; X- }0 T+ q        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
      u# f! w3 R0 _  z% H; W, X& V9 j7 a1 G- J" k8 t; c! Q3 o, }& r
            It acts as the stopping condition to prevent infinite recursion.
    : A( Y8 O9 j/ [# H% D8 W3 v
    9 t2 y3 V6 B( T  [2 A3 {9 W        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    6 w6 F" Q# o, @+ |; Y+ H% z
    ( z6 m! U1 a  I8 W# e, c    Recursive Case:
    0 }1 V7 d; F& f) z+ J# N! C+ F
    9 M; j- v  i: z        This is where the function calls itself with a smaller or simpler version of the problem.
    / u& F+ A$ {. q( Y! d+ W6 B7 U$ s
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 C' v1 l: Z' r4 @% T$ s, r. z7 |* `3 g
    Example: Factorial Calculation2 L* m: V& b6 K" x( q
    % Y2 o) S/ L) V$ A# e3 `' O: q; D
    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:
    $ F  C4 j; T9 Y7 s
    / G# E- f# }& p    Base case: 0! = 17 Y0 @* E* @# s+ p
    5 q& ~* b( S4 m5 H% ?! x1 k+ [8 `
        Recursive case: n! = n * (n-1)!7 r7 |% b/ l, V/ x& p, l2 Y2 c

    / N$ L8 D2 n) B' ?( u) S4 p0 THere’s how it looks in code (Python):" U' E5 C; `( h4 }) d
    python
      M' F1 L- J9 G, r) d. i" m7 c, ]( D. x( [( _) a) M
    2 i/ v2 D  B, D: W
    def factorial(n):
    ; x$ C1 R8 q" `3 @+ C* W8 v9 q6 j- k    # Base case
    " |( D* j4 A" a  E/ n, G4 s/ v    if n == 0:! X" }: g) u. }/ s* S+ v6 s' L
            return 12 L4 a7 A4 E! k0 C! [5 ^
        # Recursive case" ?! R2 I- K4 J' d
        else:
    7 K( e7 S+ d0 G+ w+ Y1 m        return n * factorial(n - 1)3 e9 \7 N6 h2 ~: V! P! S; i; T
    5 a+ l9 x9 F1 U2 S! r- R/ ^
    # Example usage6 u: `/ ^& |( R
    print(factorial(5))  # Output: 120
    8 ?3 U, L$ {" a- m
    7 ]) C) `2 _; a6 _  N0 j4 F; Y  sHow Recursion Works( x. [1 F+ ~2 e4 U, i9 Q$ J7 F

    : P% J7 h/ i* q8 w; |% @    The function keeps calling itself with smaller inputs until it reaches the base case.9 {' e" k3 O  B* L

    1 m  z% F5 `+ D) z! u9 M; [  j    Once the base case is reached, the function starts returning values back up the call stack.
    8 b: X$ A% ?! C+ ]% n
    , ~& q+ ]. q( a    These returned values are combined to produce the final result.% [, F- }9 \/ a- v
    ( b3 [4 X5 f* |; E' x% j% t- `
    For factorial(5):& n( q3 Y$ L/ \" M

    ; N2 E; f( m( I6 l  Z1 j; z
    $ o5 \8 H: d# {# j! o- j- yfactorial(5) = 5 * factorial(4)
    * c8 J8 M. L9 J0 l3 G  u4 hfactorial(4) = 4 * factorial(3)! ^0 @7 `: s/ |; K2 Z; Z
    factorial(3) = 3 * factorial(2)
    ; I0 Z9 E& U) f( ?  J6 o& W& Mfactorial(2) = 2 * factorial(1)$ n# y% H  }( X& |
    factorial(1) = 1 * factorial(0)
    2 L# ~. Y3 v7 M; ]3 I% X) N7 lfactorial(0) = 1  # Base case
    , C8 \) V- |9 {" p+ {: X7 H/ t/ f, ^" p
    Then, the results are combined:% ~4 N# ]6 I% v: ~

    2 u0 Z1 A6 [+ S  t8 j3 g4 b
    - ?: U9 C% S0 H$ }) efactorial(1) = 1 * 1 = 1
    0 J- v4 Y& [. ]6 U/ J! Ifactorial(2) = 2 * 1 = 2
    . `0 ?' ]$ l4 M8 Q+ K1 \factorial(3) = 3 * 2 = 6
    5 u" @. G8 Y4 d+ H1 afactorial(4) = 4 * 6 = 24
    6 S% t+ I  W- C0 Qfactorial(5) = 5 * 24 = 120
    1 k9 v! \" X, V/ @! W) z( c
    8 _/ H7 v& T7 l4 f, VAdvantages of Recursion0 p, _, ?5 n! y: P3 |
    8 F. m% ^: \- j4 p
        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)./ \+ Y5 _$ x0 `* r
    & z2 R) `" y5 P  c( h
        Readability: Recursive code can be more readable and concise compared to iterative solutions.; y: w" r! f5 j) c9 ?

    / P3 w/ J1 j1 v" s9 T9 iDisadvantages of Recursion
    1 f7 t. O& Z( Y3 Y, u: g9 Z! J
    3 c  k* \! s# y/ [/ E    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.3 e- H  \6 I! V  c0 D9 g

    : {1 [, E) S/ Y% N  n) ]5 U    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    , @6 K. L! R; G# Q% b
    # I' w+ b9 R% N& @+ JWhen to Use Recursion. {& d! M& g- u  D* H9 n/ K$ u
    / U. f* Z+ ]# B2 d$ q$ H
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    7 \8 A# U: i8 Z* Z1 l" V5 G0 m" `% u0 Z; z* _
        Problems with a clear base case and recursive case.9 @* v3 p% f/ s5 ]4 X4 a7 t9 t
    ' Q. I* c' ]/ O( m" O/ H8 d+ n, P
    Example: Fibonacci Sequence, c  p$ z7 G% D8 e9 _) F4 i! j& K

    * \+ c, {$ w1 F7 R6 d3 Z' TThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    5 P# n+ T5 @( K* f: X7 d$ y1 d5 [6 |) w* H
        Base case: fib(0) = 0, fib(1) = 1
    6 P7 W/ d2 ~( e. g; t7 c  i1 E* W5 I0 ~
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # C3 B7 u% U. Z/ g# e0 k/ w& A! ^
    python
    / A' {% i: m1 [
    * }$ V* I4 q; F7 w5 _) G' d; P6 c6 y* Z% B- N# E6 S7 i0 V7 O
    def fibonacci(n):0 O- v0 G! d6 `
        # Base cases
    4 M1 M" ]% B+ L+ `    if n == 0:
    ( ?3 p" b4 v7 p. J        return 0
    " K; |5 B+ Q. g2 i    elif n == 1:% a# v# L% S9 d/ b5 l0 p
            return 1
    1 R4 q0 }+ y9 M+ p# y) ^& e# t    # Recursive case) ?9 f. ?9 h+ x
        else:! p5 Z2 S5 T9 O$ L" E8 I& }3 a- h. T
            return fibonacci(n - 1) + fibonacci(n - 2)" A, L& O& k5 k( C1 x, I
    - g9 b/ t' r+ D  c! x
    # Example usage
    1 P7 n# ]" V( ?+ s7 Eprint(fibonacci(6))  # Output: 8- N2 a5 Q& B) z6 s: ?# Y
    5 N2 C; M; t, {$ n/ X& j
    Tail Recursion- m  a, F" I/ x2 o0 O% B9 X
    - r, V% m; p( C8 ]: P
    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)., b4 m  n7 U* |6 B6 ]
    # u- R$ `  R5 E& M( Q7 @0 n/ u
    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-22 19:30 , Processed in 0.062733 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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