设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 . N& T6 C; i, ^: {# M# {/ k

    2 N% |$ u7 u( ~: U1 \解释的不错! D: A9 k, M7 s6 a' E4 G" }4 ^3 B
    0 F1 z1 N( ^/ O; ^
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。( t3 _+ R$ `4 I4 \

    ' Y; F% L% b+ ]0 ^4 m8 j# C+ ~ 关键要素
    4 R; l5 D- F5 S/ u( L' e1. **基线条件(Base Case)**
    ! h' R# K4 L; J9 p4 Z$ f   - 递归终止的条件,防止无限循环1 ?* I5 A$ e9 j5 C/ F
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    $ b. y2 C9 T( J. c2 ]5 _4 q* Q# i; O
    2. **递归条件(Recursive Case)**3 i2 B, u! m4 }) b' m, X3 @
       - 将原问题分解为更小的子问题9 q* ~1 k  ^* f" X# M, k- A% p, @5 a
       - 例如:n! = n × (n-1)!8 O) y1 P& M" ^. D, D# C. a; y

    0 d' E% y' x! z4 O. `" j6 o 经典示例:计算阶乘+ z* A* h: p5 P7 v) }
    python! Q" ]3 C4 T' b
    def factorial(n):
    " ?  E1 l" x6 k9 ?$ }  }    if n == 0:        # 基线条件# b+ T* x  |! A2 g
            return 1
    6 Y& Q9 B9 G# v    else:             # 递归条件
    ; R; m" l2 D' \! Y        return n * factorial(n-1)7 o# F! m. ^1 Y$ B' o
    执行过程(以计算 3! 为例):
    " w+ l- O: q+ A- Kfactorial(3)  r$ Z2 C  v! S/ {) j
    3 * factorial(2): t( v! F5 I  V
    3 * (2 * factorial(1))6 z3 S* r! [% W4 q! J7 e1 T/ o
    3 * (2 * (1 * factorial(0)))
    ) R( f$ P9 k, W. K; _- z3 * (2 * (1 * 1)) = 64 I$ j7 Y3 Q- `
    : i2 S; n& I0 r. ]9 X( k
    递归思维要点6 y8 M" Z9 ]  |% W$ a
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    + T! W' m3 C' ^% w( y+ i2 i2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    , p2 @8 e1 l8 v( y, e3. **递推过程**:不断向下分解问题(递)  J6 n1 Y8 X+ I" |+ L
    4. **回溯过程**:组合子问题结果返回(归)
    3 J" y7 y$ C* H, E$ L
    " y. s6 @! X8 @6 m6 c, b  D+ x: v 注意事项, b# I1 W" p# Q0 A
    必须要有终止条件
    ( i2 R3 c6 A4 S0 s递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 J4 E, k6 H0 p9 o- S% \+ h3 T
    某些问题用递归更直观(如树遍历),但效率可能不如迭代8 i0 O8 L: N7 u' M
    尾递归优化可以提升效率(但Python不支持)* m% M" X; R: z2 e1 \2 d3 N
    ; R! q8 m# P! K+ M+ K, U2 c) [
    递归 vs 迭代
      U, h& |6 a4 u|          | 递归                          | 迭代               |
    % ^1 N+ V, z( k& F$ ?|----------|-----------------------------|------------------|& I, I* c4 h- W5 f! _
    | 实现方式    | 函数自调用                        | 循环结构            |( ]; C+ W1 N, n# E$ `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    * f$ {1 z2 W. T, T( D" u| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    + L; H. Z( t$ E! B9 ^# a| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" t6 c$ N$ z( B0 @: h+ k1 W8 c
    8 F& ], M# t( J. L
    经典递归应用场景
    , {. N: N1 m+ w8 {) p; e1. 文件系统遍历(目录树结构)- U4 t& U* k4 `) L' L
    2. 快速排序/归并排序算法; r+ s6 Z1 `8 `2 S3 h+ q' x
    3. 汉诺塔问题
    9 M4 x3 O2 T# W/ x1 j6 g# D% [4. 二叉树遍历(前序/中序/后序)
    ' u: g- N7 h* `- x2 [  C5. 生成所有可能的组合(回溯算法)
    ) b. N4 z( P0 a  [3 G# p
    . T$ h" P- h7 Z5 A0 j试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    11 小时前
  • 签到天数: 3301 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 e$ |: Z1 [/ R! y- n- N
    我推理机的核心算法应该是二叉树遍历的变种。
    + _4 [2 z" U& E, t另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    0 @. ?. d% N+ p7 \: ^Key Idea of Recursion1 l& I8 q) o2 R# g8 {" z5 V

    . ~1 j1 \& F& B( m: EA recursive function solves a problem by:
    5 L- M. D7 u7 \6 L3 B4 g* o# |! r& Y* A1 r0 X
        Breaking the problem into smaller instances of the same problem.
    , u8 C0 u5 N1 K5 h/ J+ o9 {0 k3 [1 l. u9 f6 d  X3 e- y, y2 |
        Solving the smallest instance directly (base case).( K" S3 R8 P% Q2 Q
    4 T* L4 q6 D  L* I& U( O
        Combining the results of smaller instances to solve the larger problem.
    9 T2 U: ]3 q3 p1 O; y+ F' C* @% K7 v5 a2 z- v" g
    Components of a Recursive Function
    7 z! p) y* c9 V! D& Q2 \7 c$ n! A* v; l3 @3 S
        Base Case:
    % S: [9 O6 W+ x" m9 K; i/ n; V6 B( p' d9 T" e0 n" T8 ~
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.( _+ S- R( @7 s6 Q7 {
    7 ~( P! D2 u4 U+ a9 T
            It acts as the stopping condition to prevent infinite recursion.
      B0 D9 Y" X6 w; u0 r) Z; f# L; i% c6 B* _/ G* Y% i9 P6 F
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 z& [4 l  L: E! I# o$ ^6 A

    $ t+ l( ~" s- ~5 l: k( ^7 p    Recursive Case:% t6 o3 J1 K3 ?6 M4 T
    3 N7 X- F9 E* T2 O! \
            This is where the function calls itself with a smaller or simpler version of the problem.5 p5 q7 r; K7 P4 s
    0 U# j/ Y! H: P
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    . `8 G% J) S' C, x
    & U; T# _! Z& wExample: Factorial Calculation! }% Y% {( I, n
    % m) p8 u) u% `
    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/ T+ W% @$ W; h9 ?+ ^6 [( f

    : p* s  P, {4 U    Base case: 0! = 16 r" o4 A; Z/ u! ]5 m2 q

    / N2 t7 F: ~/ {) j$ ?    Recursive case: n! = n * (n-1)!4 `; b6 a! ~% Q: A% O; L
    , ^& K% _) O8 n! U
    Here’s how it looks in code (Python):
    / J2 O+ C* L4 P% S* I0 P* U- Dpython8 d+ U( n" x+ Q, ?5 p: g  H

    ! T! B) F3 ]2 H; D, t: W% _; |
    0 e2 d4 }9 |( D% pdef factorial(n):
    $ `9 y% e0 j$ ~! t4 b9 h8 D7 {& s    # Base case; K3 w, V( e% t1 [
        if n == 0:
    " |0 C* @: y- E( f2 m        return 1
    - M9 T, k& q7 b$ O  H    # Recursive case' u4 N; Q4 X0 q/ ]
        else:
    " @) h5 t) ~1 o4 Z        return n * factorial(n - 1)7 W  K, D1 C: x* j: I
    7 Q+ K* f. g# j) A
    # Example usage
    2 I! b& [6 g* P/ uprint(factorial(5))  # Output: 120
    5 I5 m! F" |; J% j, m
    8 X: f- b+ |: cHow Recursion Works
    ! F9 U5 g/ A3 s& Z  N* U; c; T/ n3 m6 K
    2 k$ m1 L5 Q2 `7 b, X    The function keeps calling itself with smaller inputs until it reaches the base case.7 K6 R2 c( @( d, f
    . e4 y; x6 ]! d8 i( b+ X
        Once the base case is reached, the function starts returning values back up the call stack.  x% E6 P. h+ ^

    # v/ G; a4 h% z4 I0 w8 }! P1 `8 c9 I    These returned values are combined to produce the final result.% |8 ~2 f+ w6 s- T3 b% {

    " X* @, o) r. W: r9 C2 TFor factorial(5):
    4 f! ?% D# H# N) s$ j
    & j& x8 |* _, |# v: w
    * ], V* n; m! }+ Wfactorial(5) = 5 * factorial(4)
    7 k2 V8 x3 E* L9 v" G0 h- Zfactorial(4) = 4 * factorial(3)
    - n* B5 l) \6 E4 {$ Bfactorial(3) = 3 * factorial(2)
    9 n8 \' i$ G5 L2 Zfactorial(2) = 2 * factorial(1)# ~& Q" h, a5 m/ ?$ t7 C0 s6 o
    factorial(1) = 1 * factorial(0)9 U; R9 ]/ K; W0 g; H& O, U5 `
    factorial(0) = 1  # Base case8 ?% |( o% H1 n6 r" [5 u
    3 U! z" m$ L( Y
    Then, the results are combined:+ H' [) R0 r  y; ]
    4 z. {8 F: U6 T- n$ F: b0 @! t1 @

    9 }3 S7 v% ~0 A0 B- Z7 Pfactorial(1) = 1 * 1 = 1
    1 T; i& z4 g! `1 Kfactorial(2) = 2 * 1 = 2
    9 W+ y: ?; e( z6 z* }4 k$ sfactorial(3) = 3 * 2 = 6
    3 t4 U9 _* c  X1 [factorial(4) = 4 * 6 = 24
    # s1 m) Q, B$ t& ]* c+ mfactorial(5) = 5 * 24 = 120
    / y2 N2 ^5 u* J7 g8 z/ o! B, S
    ! I* H2 C2 _. K- O6 t: m* FAdvantages of Recursion
    ) e+ C9 S5 B" @2 M5 E
    8 Y, w. ~+ W; V% ?) C* x- 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).
      y& b. F2 }, z4 Y1 [8 _7 k  ~. k
    ( V. I. v$ p! z8 _2 [% {2 d4 d    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ! k/ C* ?5 w3 b! P! P$ [) t5 B: N$ N+ o, ^( d
    Disadvantages of Recursion. @! J+ f$ X) F3 K) H

    4 q( ]& \! M7 x& m6 z2 ~    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.
    8 m1 [/ X0 L) `0 {& [! [$ |9 T. M* r% ^7 t
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 S8 @# e5 _) ]" B
    # y7 T# g/ y/ T. \1 PWhen to Use Recursion7 P; I" z5 `# ]( t: P( ^

    3 S8 z. t0 z* M    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( ^3 Y) U0 s! J+ b6 N9 q
    ' e% c/ S) _( l; B9 W- L: |) a
        Problems with a clear base case and recursive case.
    1 ]6 {( O, D. l2 S# o
    * G; ]5 k! j& }; Q2 D- HExample: Fibonacci Sequence  j6 n& _9 M: {' V+ A% d# r

    1 _7 H4 A, f  ]8 J0 RThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:5 v/ ^/ U3 i. q" t9 B! e& S2 M. f
    , v' }- r% f- W" H8 X
        Base case: fib(0) = 0, fib(1) = 1
    ) O0 E( ?# _/ `! F9 h/ d. o3 @% y1 B0 n$ W; v, Y1 \( u, w
        Recursive case: fib(n) = fib(n-1) + fib(n-2)$ ~4 K3 ]) p& R. I

    5 |1 ]+ _2 Y0 F2 {4 h: ^9 `3 Q$ Lpython
    9 H3 q3 A& w  g' y
    ( L3 I$ v6 T3 r# c- u* t' A# m! Z  O. u2 O
    def fibonacci(n):
    , O$ f  _; W/ h# W/ i    # Base cases
    ' ]$ ]2 D$ D+ _    if n == 0:
    3 T. |1 {* [- \2 R: @        return 0
    ' K7 n0 E& ]7 T- @    elif n == 1:
    % b/ |- x9 e$ B1 L& ?        return 1% h* b' S- B* _: d; g0 k0 I  W
        # Recursive case
    ; V7 F1 M, v3 M: k- B    else:* @& v: w" C% Y( L
            return fibonacci(n - 1) + fibonacci(n - 2)3 B, x6 @* b" d3 z' b
    . e5 r$ G  y" r4 f
    # Example usage+ `' n, R9 ~8 j& f, Z
    print(fibonacci(6))  # Output: 80 u. M4 Z# J! a5 z9 \9 f

    $ Q# l' |' M8 `7 `Tail Recursion
    . Y9 V1 l, V9 E4 D* @# p
    : |6 k) {2 O& ~. f9 c. WTail 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)." l/ P) X- m5 y& C7 m

    , e& |( G) L' }5 R% c7 u+ mIn 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-7-20 20:54 , Processed in 0.058668 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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