设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 / M+ v' s) h# ?! ^! z
      ?* B+ E: A4 j! g8 r' D
    解释的不错2 W  M! N9 s1 T( T/ V( c% W
    1 t# ?" `; u0 k9 z3 |
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    5 b0 c( @( o, }; Y  p( K' F, t3 B$ v' ~
    关键要素
    $ w: I5 o( F, l  r- c/ p( o' h+ K1. **基线条件(Base Case)*** Z' a0 h' G4 i8 X: m# Z" ^) X
       - 递归终止的条件,防止无限循环7 k8 \9 `  P6 j4 I( s
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 n: i! n0 r: l$ f6 l5 Y$ N  q
    2 a3 ]8 t  V( l8 X5 N, M
    2. **递归条件(Recursive Case)**
    . \% m" J7 s: n& h) m& [4 H3 `% Q   - 将原问题分解为更小的子问题3 t! t: P' l! }+ \: b
       - 例如:n! = n × (n-1)!
    % S% A2 L! ~- r  L4 [, T6 y9 f2 s: J5 c1 F1 F4 f% O" [
    经典示例:计算阶乘
    & v& t0 V: T; f8 h1 a9 dpython1 I  g0 o, F8 t' [' \9 w7 m
    def factorial(n):  J, s  E" m1 U2 S  \3 a
        if n == 0:        # 基线条件3 J1 j2 ^8 {3 L, _( K4 x9 [
            return 1+ Q8 U6 P2 d# Q1 m3 S- N
        else:             # 递归条件
      S2 i/ \) `, t# x5 h        return n * factorial(n-1)2 M" N8 K" ?! [' w+ J
    执行过程(以计算 3! 为例):; A0 g' T. c% r5 Y& t
    factorial(3)" W, p" j4 T+ H% k" p
    3 * factorial(2)
    0 A6 s4 O. g3 V* B. `% k9 r3 * (2 * factorial(1))
    . N- G# k7 k5 q* u5 n" N3 * (2 * (1 * factorial(0)))6 x$ `; y8 v; V
    3 * (2 * (1 * 1)) = 6. e3 j  B' {" ~) L$ ]$ n
    6 S0 N/ m# o. H# B% J- P" v0 u5 j- b. G
    递归思维要点- U9 V! `3 `: J8 n! s
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 ^5 H7 G, v6 f# ^2. **栈结构**:每次调用都会创建新的栈帧(内存空间)  Z" S1 Z; j& h. c+ G
    3. **递推过程**:不断向下分解问题(递)
    3 a4 G) }8 }  z4. **回溯过程**:组合子问题结果返回(归)
    ) Q: E' T8 x& C7 a( Y( N# ]7 A
    0 O: y" S' K; A1 D; s; g  ` 注意事项
    6 s) Z$ F( ^& h" G- ^必须要有终止条件' u. X8 l% h3 h3 F% w& |" @+ g7 k
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & H% R8 T" j- ]3 u0 H某些问题用递归更直观(如树遍历),但效率可能不如迭代2 F# }; l) v8 t6 W
    尾递归优化可以提升效率(但Python不支持)6 M( ~6 M  w( P8 o* Q

    ; `" c6 J+ d$ P3 v" o3 ^ 递归 vs 迭代
    , D6 R6 u$ a0 Z' x% ?, n# K# y. V|          | 递归                          | 迭代               |7 A5 ]$ I" c! m. q( I& t
    |----------|-----------------------------|------------------|# w' Q' D6 g% `# D3 k
    | 实现方式    | 函数自调用                        | 循环结构            |7 J8 b2 a$ x) _7 U9 t
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 \% Y; _5 K4 e3 Q) b
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ) c9 L9 N7 ^2 E+ X4 R( Z! N7 L| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 q% i6 ^) ?7 ~
    ' d, U+ [2 a  K, s  t
    经典递归应用场景5 w# X3 ]( z+ {# D
    1. 文件系统遍历(目录树结构)1 K9 r4 G0 f& I. z3 ^7 u) B) N
    2. 快速排序/归并排序算法1 `5 S5 Q' F* \# Y3 A- f
    3. 汉诺塔问题
    * F6 y$ r  Q- c% Z' O( t+ z4. 二叉树遍历(前序/中序/后序)
    6 X3 n4 M$ R/ F  X# @/ r' a5. 生成所有可能的组合(回溯算法)6 J* R4 C* o; V4 K

    ' Z3 E1 |/ x$ B0 r- ]* R  s# c! m试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    3 小时前
  • 签到天数: 3108 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,* Q, M- \+ j0 e  D% c0 V
    我推理机的核心算法应该是二叉树遍历的变种。1 Q- U6 o/ q8 R2 e
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:) M7 K0 h$ K  g6 g' Q
    Key Idea of Recursion
    2 u8 f$ F' c8 U" F+ B  z6 g
    4 n9 L. O+ E: Z. H, O. M$ E" ^A recursive function solves a problem by:
    ) c- j( g7 }% e) C$ L3 V" K! c6 K0 b6 q7 P2 h% A1 H: `
        Breaking the problem into smaller instances of the same problem.
    5 z/ H+ P# H/ q, o0 p* i9 z; B
    - l* i1 T+ K( Y# ]$ ]* |    Solving the smallest instance directly (base case).
    , O  L) \2 y5 g# v3 C& q: x8 F' Z6 t' {% b; o% D
        Combining the results of smaller instances to solve the larger problem.( n% L- ?$ l0 R

    & d* |5 C0 H$ S. ~0 Y3 T' E- k5 kComponents of a Recursive Function
    * Z. K! B4 f8 a! y# P9 `: y; W
    & i5 ~8 g  `" V, z    Base Case:9 i$ M: Y! I: h  D% i2 x. }8 G
    $ ^) [$ }7 V* I1 D# z" Z
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ' u  v$ j0 Z8 }' I0 {7 }
    " S) |, c& i- E9 }- ?0 K) ^) B1 L        It acts as the stopping condition to prevent infinite recursion.9 ^) V& N9 Q" U6 t) @  H

    * V4 P4 m' n" n3 z2 e1 R1 i        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.$ |3 B) |( B; k! L) W2 M: Y

    ( x5 l& A5 ?$ a  P% k0 T( c    Recursive Case:/ D/ [+ r1 M" Y0 x: ^2 E
    5 L9 b3 X' v; _; o. _. ?; v+ ?
            This is where the function calls itself with a smaller or simpler version of the problem.9 Y# E6 C* }! V+ b2 \0 C
    1 T, i9 s) X( @' v# B6 c
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 e8 `! x' K" J* z1 m9 Q& E" p3 `% I
    Example: Factorial Calculation7 a( u- {" p8 M8 @

    , P' [  o( l: g& iThe 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:
    ' p6 v7 P  z5 [% _5 v. ^  `4 H3 J! C" W2 @" x" ^; c4 w
        Base case: 0! = 1
    * L$ w/ G: h* q+ C; |% R6 m* }9 G- P2 t( L% e! c
        Recursive case: n! = n * (n-1)!
    9 C2 O9 \" ?2 I* B2 N: U. Z9 e; V1 ^9 J7 h
    Here’s how it looks in code (Python):* _& X6 t: [; d6 b
    python
    " f! p  d" B; @" @* x& C- [! S% ~7 @/ x- M7 Y

    ! h* i1 A4 r& u4 K" Gdef factorial(n):& r: k  n" j  [$ G
        # Base case8 ?: r: u: T2 X; n
        if n == 0:
    2 o8 {  ^4 g" i        return 11 M# \) Y: d3 G9 v. F- J
        # Recursive case8 H0 j8 c5 e3 {5 t9 @
        else:4 k1 ?0 {; Q- L: n6 e) u
            return n * factorial(n - 1)  P$ c1 w. x  L% W+ [: [" C

    6 A4 C/ [  [# l1 j# Example usage$ W  W; ^/ r& M  x- r7 G- w
    print(factorial(5))  # Output: 120
    ' p0 }6 A+ S1 O# c9 q; @1 l6 X
    ! L+ V. H2 D7 CHow Recursion Works$ g5 V3 g9 @- U, k! W
    4 x* L7 Y' T8 k8 S: B9 S
        The function keeps calling itself with smaller inputs until it reaches the base case.% i9 ~2 `" R; Z  }/ R  S

    1 b5 d* B" ^5 X' X$ E    Once the base case is reached, the function starts returning values back up the call stack.* f  i# Z0 D0 r$ D

    ( \, v2 N1 ~7 T& l) x; y    These returned values are combined to produce the final result.
    ; h: X6 ?/ Y$ O3 S$ J3 z+ d0 m9 a9 t3 [2 b
    For factorial(5):
    ; |/ J# ~8 X8 T/ E9 I
    ' T0 B' y; a7 n. _- v! l
    & O8 f) r  N3 F, _) v/ d) xfactorial(5) = 5 * factorial(4)' z! A9 w6 E" r4 _: f1 {3 B9 K
    factorial(4) = 4 * factorial(3)) s/ b% }$ T" I1 S5 w
    factorial(3) = 3 * factorial(2)5 b& n) K% D8 a0 \
    factorial(2) = 2 * factorial(1): N) z7 M  L2 k3 P0 t
    factorial(1) = 1 * factorial(0)! ]6 V6 Q- t4 e
    factorial(0) = 1  # Base case
    6 a( r) [" T8 J! u# e% g9 f  m# I" {$ I  S
    Then, the results are combined:
    : [4 M# n3 d' }# o! x
    : H) f5 f4 `% b+ i; L9 I- L2 c$ h( m& X
    factorial(1) = 1 * 1 = 1% C3 t# H5 ?$ [# }5 Q7 ^
    factorial(2) = 2 * 1 = 2
    ; h3 e! f( ^  p& Lfactorial(3) = 3 * 2 = 6
    0 `/ A$ z+ w+ Z4 I; [! M, efactorial(4) = 4 * 6 = 24% s, P6 M, l; D# }
    factorial(5) = 5 * 24 = 1203 ~1 V0 Z! d9 G  H3 |2 ]+ \
    & u  N- q) f' G5 K2 T2 T% v; v
    Advantages of Recursion
    4 N- |1 [! P; L& e2 h6 w7 c) ]. O
        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).
    : C$ V7 m9 X; O1 H0 L1 j2 `! _" @$ ?% b5 e
        Readability: Recursive code can be more readable and concise compared to iterative solutions.1 K, P: ~3 I* `0 _
    6 [0 S& @: Z; |2 S9 V/ D
    Disadvantages of Recursion% M8 E+ a) C) e

    7 n) o7 H( Q" U! {* l* t* _9 [; c    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.
    . V" i% f2 ^" Q0 l7 @% t0 x* f
    / y# e" P* r2 x5 x! K5 y; \4 E2 Y7 @    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& s4 c1 A8 s2 r! @6 n8 ]; Q
    ( P# N& ?# I/ K" r6 l' d
    When to Use Recursion
    ' v! z9 Z0 A; f. x" T$ R  _5 S7 n& Z8 c
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- C" E( b. |3 Q6 k1 \8 ^" l% c6 J4 g

    2 P& x4 u2 s  _8 G+ C: E+ L# u( [' ?+ m    Problems with a clear base case and recursive case.
    9 F5 ?1 q& ~; N  g( S) ]& X5 j4 w
    * y0 w$ M* c' t0 R7 D5 }3 B9 fExample: Fibonacci Sequence
    " ?5 t" J( I- e: N5 B: y! I- Q9 H; V, g# x( p
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:' C" ]$ V; z/ a1 p5 U7 [" |; ^

    & a7 d5 s% c( H# s; j# J    Base case: fib(0) = 0, fib(1) = 1
    ( I& W; R  O  a& S
    7 d6 {  l, \& M9 a2 h; W    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' k' b( d0 W6 Y& d, A- Y5 S
    . F" @9 V$ ~8 i8 {python7 T! o. x; d0 K$ L( z$ S# d

    $ G5 F9 \+ u7 B! e
    3 F& t  E" T( N; r8 }8 {& ~& cdef fibonacci(n):
    5 k$ @% K6 y* J% w    # Base cases& c0 N( {' y7 |
        if n == 0:/ [: C: C* m3 w9 u' r* S
            return 0
    + ]( O& L- u3 X7 d' k6 [; k    elif n == 1:
    6 d& K, Y0 L; \7 G" @        return 1
    * P7 H9 g$ B, \    # Recursive case
    . c3 i' m, E+ S2 d' Z! i$ e( w3 b2 b    else:
    3 h& x2 A. a- m. w2 h# N7 s        return fibonacci(n - 1) + fibonacci(n - 2); N1 `9 ?& l8 }

    0 w# y; C3 [) b7 Q8 g# T/ U! t# Example usage* R0 |7 @2 J8 K5 N. @
    print(fibonacci(6))  # Output: 8! n( U+ v/ G. @+ ?6 w. D

    8 t" V! x! H9 a4 [Tail Recursion
    ( X, w+ A7 @( @8 v% i) n! s8 g( F5 q& J5 l  t9 W
    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).
    ' ]6 {& V# o" t" m: A; {+ B3 h3 j
    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, 2025-12-4 15:01 , Processed in 0.031049 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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