设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 " B! `7 p; ^& x% ^1 Q& Z. h

    5 J( I1 C) h$ V+ {/ C) m" W解释的不错
    * g$ r0 N0 b5 v' ^4 }: f3 q
    ' e% w$ {8 v8 y8 P递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。; D# N$ g+ J) G7 z* s5 b
    5 {5 g8 H% T$ \; c: `# G- S
    关键要素2 ~% _+ n  F/ K9 T6 `% z
    1. **基线条件(Base Case)**
    9 `: ?- r! N) {" N9 ~4 k1 {+ C2 h   - 递归终止的条件,防止无限循环7 T, \, T, b: x) f. y4 f% T
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1: w4 ?( c' v+ d

    % [" |! p6 b, m! n0 _# m2. **递归条件(Recursive Case)**
    % u+ O# T& L4 K$ [! u7 Z) D  a& b   - 将原问题分解为更小的子问题
    - Y& S; O/ g- f, n% R$ N5 D9 f   - 例如:n! = n × (n-1)!1 b7 a# P! m2 a" h4 h

    & \" x7 u5 J8 F* P  E 经典示例:计算阶乘: I, @2 |2 h3 V* {5 w. c
    python; B' O8 s( W: @$ j4 p. b
    def factorial(n):8 F( f# v, d7 E) e8 u4 [. A' g9 V- G& t
        if n == 0:        # 基线条件6 z' g) u9 X* A4 U$ t" u
            return 1
    - P5 E  g& a# a) Y# m    else:             # 递归条件
    2 C" h0 [% Q9 f, X# N9 S        return n * factorial(n-1)
    $ W+ a+ [* m8 I, f执行过程(以计算 3! 为例):
    ! O/ q! c0 h* P4 s2 T! Gfactorial(3)
    4 W/ {9 G# R5 l# b  d, R  F3 * factorial(2)& X: c6 B5 [. ]
    3 * (2 * factorial(1))
    , i9 u  y, O, n# _: r3 * (2 * (1 * factorial(0)))
    * a0 E. j5 X; w: Y% v& d2 s5 c3 * (2 * (1 * 1)) = 6
    ' x% w. F0 R/ ~  }8 {) m+ P  k# o& ?: l% ?; Z# p1 ]# o8 n
    递归思维要点
    0 Z) g4 O4 Y; f. k4 I1. **信任递归**:假设子问题已经解决,专注当前层逻辑1 V% j0 j1 t* ?1 s+ T
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    2 |5 s$ Y8 L+ U1 |% l% `3. **递推过程**:不断向下分解问题(递)- j0 I& O! K6 X, o# U
    4. **回溯过程**:组合子问题结果返回(归)0 y! J8 j0 ^# T7 U% x2 h0 y& }
    0 ]. H& y4 w; _8 i: w
    注意事项$ \, R4 i1 \, K
    必须要有终止条件7 U/ V6 {* B6 q1 b: u
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    9 y1 E2 P% S- I$ h; r1 p, x: E某些问题用递归更直观(如树遍历),但效率可能不如迭代9 c! u% z; s+ x* ]
    尾递归优化可以提升效率(但Python不支持)0 r. T) H* H6 z' p; i) _& _

    / r  m' ]& g* |9 A, C. \* d6 r 递归 vs 迭代* E: m: g  q, T" f, J& n1 L* Q
    |          | 递归                          | 迭代               |
      l0 K0 M: \# K, Y5 V6 d) M. G|----------|-----------------------------|------------------|
    ! ~3 a& N+ W! Q& s% Z| 实现方式    | 函数自调用                        | 循环结构            |
    & s7 n: i. C: \( Y| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 Y: o  R; u& e4 s/ p
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    - H- H: `$ Q1 y, F: ^| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 i2 q, E) K# u  W- H  D1 R
    . e% {0 r  i) r 经典递归应用场景
    7 v# t; X% m7 p6 T! X6 M1. 文件系统遍历(目录树结构); r; u9 P/ T# {3 g
    2. 快速排序/归并排序算法
    0 j: o) r* ~5 W2 V4 E6 C; ~3. 汉诺塔问题4 J/ m* A- G0 `) c  E. o7 S" J
    4. 二叉树遍历(前序/中序/后序)
    5 M+ y  V2 d& ^5. 生成所有可能的组合(回溯算法)
    8 h3 A+ T4 }% W( K9 d2 |
    ; J; Y8 W: {/ {# a% ~0 U试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 z& F/ m- z# W, x2 ]8 j' _我推理机的核心算法应该是二叉树遍历的变种。
    , Y* G- M8 H4 [' L# Q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 j% w. M, }  T) R* j9 eKey Idea of Recursion$ {" C) L" J( Y$ R2 `

    4 N# m- B8 U) U  f% d6 M( X3 MA recursive function solves a problem by:
    , [! E% I9 p5 O& c$ p5 l9 {9 Z( ~, l# m3 g
        Breaking the problem into smaller instances of the same problem.
    0 Q. w2 O) q2 T7 {- Y1 U2 V+ J% d* V- U
        Solving the smallest instance directly (base case).( L/ @8 F; x4 ?& a0 y' C
    ) l1 j: D6 t+ X4 a; z8 c
        Combining the results of smaller instances to solve the larger problem.# i: U  W( P/ U: k2 R" P) a8 ~, Q
    . D( ?6 J3 i1 j# I" T* h1 @4 R5 f
    Components of a Recursive Function" J% s  O" O' q  o9 E7 B( V0 W
    3 Y" |6 X) n6 n0 i/ U4 x
        Base Case:' k* r$ r8 N! T

    ; x6 P9 U# t# ^" o        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    " r. z8 ^9 Z* g2 Q+ T0 R4 Y
    7 u8 {( s3 r: ]. o" z0 R( P; L        It acts as the stopping condition to prevent infinite recursion., C9 A2 ]2 m! k
    2 e7 q& P, N% e) p/ a" V+ W# Z4 }. g
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - I3 `1 o- {% J- k# i$ h& T
    ) [4 P1 M7 h5 O0 e$ W  ~7 Z, v4 Y    Recursive Case:3 Q. j: r0 ~  H- @3 A
    ! r. m+ A! B# P  Q. [
            This is where the function calls itself with a smaller or simpler version of the problem.! I$ L. `5 h# }5 H& x

    ' `6 N2 ?1 F$ r* E! b        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    # u' B" M5 W& T+ x0 ]& _
    4 d4 D! H7 g* g; C- T1 `# @& qExample: Factorial Calculation0 f$ V% ^9 [- Z$ ?$ K/ ^# `
    , j9 h: H. E/ O, `; g4 x  x
    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:0 `+ s! z8 h4 K" e  Q, U, `

    3 I& u+ ^( t- A4 Q1 D1 S    Base case: 0! = 1! g2 a+ S! @, _8 X2 G. g
    & D6 _$ `, h- ?4 ?/ C5 E, |9 e
        Recursive case: n! = n * (n-1)!1 C6 U6 T) C( i
    % R7 G. v8 {7 w
    Here’s how it looks in code (Python):
    / V8 i- J5 a; Q, m( G' Qpython6 h0 ^/ C3 u0 }0 h6 ]2 [
    7 y- c4 _0 x( W. K* a
    & i3 [+ a- A( I  D
    def factorial(n):
    ( t0 j$ i; g9 Y/ E2 A  ?* P  f    # Base case! r. G" a) A- ~$ x- n& @7 j
        if n == 0:
    % y, K* y) G4 G9 a) V        return 13 {+ W1 r9 p0 M( f% ^1 f
        # Recursive case0 F4 B) s, f( t  @* L' S( ~& |  `
        else:
    / n  r& {5 d  q1 u) }        return n * factorial(n - 1)/ g+ n/ \5 C/ J7 v3 Q0 G

    $ t# z- ?* S2 c! O( B# Example usage7 x- F) @3 N: O( W0 ~
    print(factorial(5))  # Output: 120
    - k4 n. w; k( S2 X& t! P9 a  R" z4 E; X+ ~' v
    How Recursion Works4 U" ~+ m5 N: L+ d4 Q7 Q
    $ q5 |' d% z' u( _: q  V9 N
        The function keeps calling itself with smaller inputs until it reaches the base case./ @2 s3 k; \8 [7 T3 x
    2 |8 x  ~3 o! {* p$ c$ N# P
        Once the base case is reached, the function starts returning values back up the call stack.
    6 d2 ~4 }) G: J  F7 e' L9 h* I
    9 a$ m$ m$ h' D- L( ~) H    These returned values are combined to produce the final result.: y& B/ a  O, J) @$ O
    3 G( X' d: c# P, ]$ J
    For factorial(5):
    0 Q$ k7 j5 i; \) I8 D4 ]0 A6 p) b5 t0 v
    6 Y6 {7 _. H. N( L2 e5 r
    factorial(5) = 5 * factorial(4)
    : e3 J  n* p2 `& s, }- Vfactorial(4) = 4 * factorial(3)
    ) q& A+ x* C: y2 G' Sfactorial(3) = 3 * factorial(2)$ L: o+ h6 S; b7 E) D! e
    factorial(2) = 2 * factorial(1)& p3 U7 F' d2 L0 p" ]
    factorial(1) = 1 * factorial(0)
    8 o6 V- D+ m; b4 c0 l% D/ \factorial(0) = 1  # Base case
    & w/ [( Z. F" U4 U
    0 D; x$ k$ {* _Then, the results are combined:
    . l) W7 ]) x4 G, l2 J( H
    ! A. f, b' U5 W9 U! l4 ~2 e  G2 N8 L$ h" U2 c% W" D/ h- N4 @
    factorial(1) = 1 * 1 = 1) n( T. h8 l& @4 c$ ^1 J( I
    factorial(2) = 2 * 1 = 2, [% S; {3 J2 Y1 j+ M$ t
    factorial(3) = 3 * 2 = 6: m7 A3 S8 \' I$ J) h
    factorial(4) = 4 * 6 = 24
    $ u3 y0 q% [9 e6 G3 p' nfactorial(5) = 5 * 24 = 120
    ' z+ ~5 s# p# ^0 L0 K4 E( T, ^$ P. w* u( K5 @
    Advantages of Recursion
    + b2 d0 x/ \" [6 C3 ]' A0 [/ G6 i. s
    # ]" L6 W8 I+ Y' q    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).
    ) }5 f2 u. ?9 @: m$ E: m# M7 \( T1 T) {" u" l
        Readability: Recursive code can be more readable and concise compared to iterative solutions.- U2 Z, u2 o( J4 ?# X
    # }  a) _8 f6 G9 f9 U
    Disadvantages of Recursion
    ( g6 p3 R& e- E4 U2 d6 N
    " Z! T5 o9 ?( v    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.* I5 Z7 ~+ e' ~0 [& C2 _

    2 i' W! j9 L) l& W) z& K/ E* p    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    / |$ S4 L8 k6 E1 P6 r, F( J3 w: T6 W$ L2 ]2 A
    When to Use Recursion
      V8 h3 L$ E: G9 S5 e+ A$ s6 s' ]# |$ z! m" a) ^# A4 A
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . a# E6 V* u4 v/ r2 ~
    % w" l* o. Z7 Z' L  K. }    Problems with a clear base case and recursive case.0 z) q' p2 T1 w: g! T
    9 i0 T* F# m! F9 v* t
    Example: Fibonacci Sequence
      Z7 K$ b, G* L5 w9 y5 W
    , A2 Y+ Y' D! o0 eThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    " w' v& ?* a! w2 M& T2 X( X' ~0 E  s1 }! `2 C& k6 a
        Base case: fib(0) = 0, fib(1) = 1( M7 v% K+ z# x7 p! L
    7 n- [9 f" e3 r! Z" y1 {3 B
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    $ L& d7 {9 @- u" I& \" n
    % k4 s6 ~/ O1 n" m. ~python5 C; g2 y2 ^4 K

    + o; r0 g4 G/ B2 ?1 n( T' y+ ?
    # M0 V' [. Q5 f3 \def fibonacci(n):
    / d. W1 O+ b1 O8 z; k) g    # Base cases; S; y, @8 g: H, @
        if n == 0:( w  _  N7 ^- F4 N% U$ ~5 k1 J
            return 0
    % t; O3 M  ]. S    elif n == 1:  g- @" L" l' ]0 o, F
            return 18 i/ q/ h7 f1 e+ i. o
        # Recursive case
    " h4 j2 n. I% s' l    else:! [. K. R0 m7 y, w: F
            return fibonacci(n - 1) + fibonacci(n - 2)
    . `! K) o. J7 {: U# @
    0 [& f9 h! j1 O3 A# Example usage
    5 F2 e) G' {# [print(fibonacci(6))  # Output: 8
    , u; n  \7 |( ?3 `- B8 e1 D
    : i& {) E& m( t! G/ \8 TTail Recursion
    ; t  C3 E* ^1 ]% Y8 o# h
      z! g0 V; u+ v) ]5 pTail 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).
    8 r3 q9 T' o6 N0 @6 d9 L% r6 i
    * b5 K0 |( r  e2 p' u- T% Z9 r1 gIn 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-26 21:44 , Processed in 0.059598 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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