设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 . O* C% c6 P* J) D2 X. t
    ; R+ z* A( ]. T6 j) ^
    解释的不错
    9 H& B: z* b& G( V, a) Z/ r  h3 r" _. K0 ?
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' ?. }: a& U7 v1 C6 w( H# c. x
    3 R5 @; O% C( r+ \/ T* _# c8 a
    关键要素
    ( U% K" B# E1 v% ~1. **基线条件(Base Case)**1 `- o9 C9 m+ R2 h% A9 ]3 B0 \& X+ p: ~
       - 递归终止的条件,防止无限循环. t- B/ b4 c) i  j2 V8 c
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% c; J' p; L6 [, D
      f9 c' z9 ?( Q( A
    2. **递归条件(Recursive Case)**; T- e8 M6 r" R! I/ I$ J7 A2 e" b
       - 将原问题分解为更小的子问题0 A0 V, i# |  w2 S6 ^
       - 例如:n! = n × (n-1)!1 P: H! q2 f0 _; k" Y4 A2 f7 W
    * u+ n! O3 h" o+ J; z* a
    经典示例:计算阶乘6 H; a  y) z* M: `' J1 {
    python
    " c" {$ D* Y% _def factorial(n):5 i( ]' @7 d6 l; X8 z  C
        if n == 0:        # 基线条件
    1 p. ], ~& V; C8 q7 k- S$ ?        return 1
    $ H& F$ e8 m. t' U3 M& O7 H    else:             # 递归条件2 b* J2 f! R. F4 M
            return n * factorial(n-1)8 Q8 J3 T- q+ h1 ^. U8 o
    执行过程(以计算 3! 为例):8 ?. W5 r  p2 ~; I0 r
    factorial(3)& s: l5 D: s# n" Z5 S
    3 * factorial(2)
    8 E+ _  _" ~  C1 x3 * (2 * factorial(1))
    5 A1 K; x' R9 x4 `+ K3 * (2 * (1 * factorial(0)))
    9 @/ |* R1 T+ U, U; V9 e; B8 C9 x& e3 * (2 * (1 * 1)) = 6
    : J/ r- |* d. n% R' L5 e2 H9 \5 @( _. N. R$ n" ^
    递归思维要点$ t0 O4 k; t2 Y4 R+ I' X2 R- P
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & O  Q9 {2 \3 D: |) B9 s( |5 ~, v2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* Q9 U0 g3 X" Y0 M8 I8 V
    3. **递推过程**:不断向下分解问题(递)6 Q8 O3 P& O2 M* e
    4. **回溯过程**:组合子问题结果返回(归)* a8 E# [$ ?2 N/ d
    4 Y- V( e8 o6 P5 x
    注意事项
    $ k; n  q2 R5 D必须要有终止条件$ L4 O! L1 R( h! h0 {
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 W5 B% P& B& l: `某些问题用递归更直观(如树遍历),但效率可能不如迭代: I0 [% [6 U2 Y' _" G
    尾递归优化可以提升效率(但Python不支持)
    % L! ?% m" T3 q, S8 J/ L" |. I* d) V! v2 {. o8 ^1 n% {! y
    递归 vs 迭代
    6 Y; a* M8 S2 G* z) O9 p|          | 递归                          | 迭代               |7 R2 l" I" j0 s" [
    |----------|-----------------------------|------------------|6 Y5 R6 S4 o" K& z! T' H9 W  T  s7 h' V
    | 实现方式    | 函数自调用                        | 循环结构            |
    2 m# c0 z# u; [8 C| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 U* v- p" W9 A- j5 g8 @
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 f; K- a/ p" {6 z1 \  f| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # e+ i6 r2 G: m0 X# ^
    . O9 W( v( Q. @ 经典递归应用场景
    * B, ]& ]3 l0 A& D- r1. 文件系统遍历(目录树结构)& }( L3 ?" N% E/ T' e6 p
    2. 快速排序/归并排序算法# o; l4 U9 H: N  \& z* e+ x
    3. 汉诺塔问题. V, P& b( B1 t: v
    4. 二叉树遍历(前序/中序/后序)
    8 ~0 r$ @/ M; w+ |! T5. 生成所有可能的组合(回溯算法)
    9 X* S2 i, Z* F# u- ^
    9 j# g. q& `/ m1 s. t+ S( [& S试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    12 小时前
  • 签到天数: 3194 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% o- Q$ N6 h1 f6 U6 |' }) C
    我推理机的核心算法应该是二叉树遍历的变种。
    * N8 v3 l/ l. \. u另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    * j% h( N. A: f6 O' eKey Idea of Recursion
    5 |# H/ P" U, t0 O, g' _# W/ a9 v+ A0 D& o7 y
    A recursive function solves a problem by:
    . [. g  ^. n! @1 Q9 X3 x- F) a9 d3 z/ g4 r6 k+ U& k7 n% M2 R
        Breaking the problem into smaller instances of the same problem.: \/ Y: e% Z. H3 m8 e2 H. i% Y

    9 C/ q/ l2 I- v: U    Solving the smallest instance directly (base case).  E$ K. N) a3 g( O. q7 @
    " `0 \2 [3 d$ V0 `: R
        Combining the results of smaller instances to solve the larger problem.
    # Q) V4 e* S9 K- f5 u% |1 y' i5 ~+ r  J" e9 s. D4 C" M# r
    Components of a Recursive Function
    4 |2 \2 _  n) S+ \
    ' v0 r) {6 Y$ ]5 A    Base Case:4 K( ~/ y3 }3 D  J' ~  g: U  W
    3 c0 k4 h  n5 G3 N3 P) g7 o# [
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.3 n  ~+ l1 K( Q0 u% i$ X
    0 \9 z. G; y, ~' v5 }  L+ K
            It acts as the stopping condition to prevent infinite recursion.
    + ]: l# J: G# i% H# B* A- p  j0 U: i/ p( G
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! b: x4 ]2 @2 e2 ~& y. i: Y5 y
    3 z+ ^$ @0 P4 @. v! D
        Recursive Case:3 I! v* j# r' K5 {
    ) t6 F* X4 D( }& b
            This is where the function calls itself with a smaller or simpler version of the problem.' v# ]" \7 e) w) o1 \, ?4 b

    - v/ y' [1 p) C% ~$ B        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 Z/ I9 p9 F2 W+ F

    : t9 ]& @! N7 C  L5 [Example: Factorial Calculation
    3 |- F) g2 b  O3 [) Y9 R" g& `  M
    * h" r4 R6 w# X7 q0 o7 EThe 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 V2 c2 g9 G* z. X8 {4 K
    4 D+ I- w( C5 C3 _6 H  f+ O    Base case: 0! = 1
    + _2 ^) f% `/ D/ x- q  w& K  Y
    : e& _2 t, G8 c  `; l5 H9 h0 W    Recursive case: n! = n * (n-1)!
    + L4 _/ H+ k- N1 \& z, e
    + a8 x  @4 H, E! C+ `Here’s how it looks in code (Python):
    ' A5 \/ I0 H! I4 Spython
    ! n# l. i) s* Z& \$ W; W! I( _: W2 y9 C$ t+ h* u( }" v$ w' E* K

    " ^" T/ W: S8 wdef factorial(n):* N) g4 ~0 Q. _5 o, }8 Y
        # Base case
    : b: w% D: `9 }5 P. f8 N    if n == 0:
    0 v4 j% d0 C# R, {. d        return 1
    ! g/ S2 M2 c; x3 A    # Recursive case
    $ c6 h6 w" p7 U& _    else:' D; L' q6 I: R& C7 `9 J
            return n * factorial(n - 1)9 H" [) N/ z* J2 m6 l! W
    / h9 l! y' A2 g( t6 ]3 F
    # Example usage4 X) L1 k4 j! a$ }# x( z# _2 U! T& m
    print(factorial(5))  # Output: 120: q( W* i# @/ X
    . d" h# k- o" b
    How Recursion Works
    3 `& R0 C. ^0 r* @3 u3 ?6 F- n
    0 D3 _; |! `$ y& V2 s    The function keeps calling itself with smaller inputs until it reaches the base case.# U' U4 ]2 a$ e0 l$ p
    # \: Z  L0 y  M
        Once the base case is reached, the function starts returning values back up the call stack.3 |! y0 x8 R! r- U# x( ?% _0 O' B
    . l' ]1 g( Q5 Y: ~
        These returned values are combined to produce the final result.
    5 X, @: A- X8 Z( R* v* H3 y( {  j+ d' B& C& o( ~
    For factorial(5):1 N9 D9 q0 L9 k1 @% g" [# @
    4 s2 S2 J# G6 i
    0 s* K1 @5 d6 Y: r
    factorial(5) = 5 * factorial(4)# e) X& `9 \7 e* R
    factorial(4) = 4 * factorial(3)
    9 W# w# ]9 ?. h# [( S- ~' m3 Afactorial(3) = 3 * factorial(2)
    1 R2 P6 W2 Z. ^6 X: Lfactorial(2) = 2 * factorial(1)
    / T1 ^0 S% a7 n$ h6 q1 }factorial(1) = 1 * factorial(0)- |: z7 x9 k. d) L0 h
    factorial(0) = 1  # Base case' j6 w) n$ f# u8 M0 ^

    % F; x0 B! O4 [7 W. gThen, the results are combined:1 e- c0 ]3 n' G& a
    % h" C6 E) A  s; }$ }$ A6 P

    7 B% G0 J# r7 B+ s! ?factorial(1) = 1 * 1 = 1& a8 s9 S+ v" D' \, j9 C& M6 T
    factorial(2) = 2 * 1 = 25 `+ }- T: f- T: E
    factorial(3) = 3 * 2 = 6
    / V% N2 b- o3 B$ Dfactorial(4) = 4 * 6 = 24
    $ u; i% Z  x8 z" jfactorial(5) = 5 * 24 = 120+ Y- M8 q. @- d8 p) M7 M- E

    ; [7 U) W6 h' p/ [* zAdvantages of Recursion0 Q0 r1 \' d+ H9 ]9 B& w
    9 t9 Q6 W2 t& E: f% A6 m) S
        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).
    " w% W7 |6 [* N8 g3 A
    * u, E4 ~* e' l  x( f3 I    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / f/ j9 A! V/ {% H+ d% M( V6 u+ y1 c
    Disadvantages of Recursion  H" H9 e  x- y
    & J' Z+ ], u: r# C+ P% p3 j7 D. ]
        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.  y6 J# H2 v- u: V1 r. w
    ' G* R  f) [5 F1 A, |
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    / U# M# \0 h0 T) |1 ?9 t0 N2 s0 y* [% n- A; L/ w$ t
    When to Use Recursion; H! Z( I, H% U, Z/ `# R9 n
    % D  d% a- V2 o# F. X# W) J, N
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).. e) I: R& n% S, h7 \3 p

    : M. y; Y/ s" k9 u7 Q/ S    Problems with a clear base case and recursive case.9 P' Z$ {! A+ o% V

    ) m+ \& z; f$ M3 pExample: Fibonacci Sequence
    2 M8 E2 _0 K- l5 G4 @) \$ |. l( D# V6 B4 ~* S8 Y7 L. w
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 U: C" W. m. _: M

    5 T$ l' K! i5 Y$ f0 T6 u8 }    Base case: fib(0) = 0, fib(1) = 1
    / q+ f4 H: g/ [( m0 S6 {: S
    0 U" v7 b0 N$ N7 O    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    + g) @( C7 E1 G0 u- p& ^
    0 s: f! a* X; S8 npython; d) J/ t% f' O
    * x+ u- R: ^4 @
    9 \- b/ E, B. h& @+ b
    def fibonacci(n):
    ; }' ~1 _+ u  @( K* h. h& w# R    # Base cases$ c" @3 E6 Y* o0 H" i5 ~9 I; A, l
        if n == 0:% s( l5 o/ R6 L
            return 0
    , J2 }9 j- H* z- ^    elif n == 1:, _5 H$ X* y+ S, x7 p! K* H/ R
            return 1
    ! b8 N. M) [  j9 {- b$ T    # Recursive case
    & _! e& \4 l* w9 K    else:
    0 L& {- g3 M/ R        return fibonacci(n - 1) + fibonacci(n - 2)
    9 Q+ g# r6 S$ G- Q* e' [& w) ?
    # Example usage8 V# K! I5 O$ }; B' |
    print(fibonacci(6))  # Output: 8+ L+ D/ k# S- B) t$ f9 ]$ }) d

    6 D5 i: l6 ~8 S( {( A* d* W8 fTail Recursion
    # C- c& X1 ^5 e7 E5 ]+ C# w# H( Z: U4 f9 |/ Y% z2 ^( V# c; o( ?
    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).
    , c1 X2 L0 k4 u  r) s' W% z% T& f5 U1 ^' G, b
    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-3-7 21:21 , Processed in 0.063441 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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