设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , V, N2 O5 C; Q7 F

    ) _  F  X0 R" l; j9 C解释的不错
      i6 ]6 u0 y4 L4 H4 }; m/ `# O/ J+ o8 w* [
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- E$ r% o( l" P% l4 C
    , E; X! w. T: L: [* ^# D
    关键要素
    ' l7 H4 e3 L9 b1 u1. **基线条件(Base Case)**) A2 i- n" j$ F2 \  P
       - 递归终止的条件,防止无限循环
    8 l' i* I$ P. L! h4 m& h7 @   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    # |: O' I4 z2 V, E# v
    & ]' a) X& Y5 j# \2. **递归条件(Recursive Case)*** {" t9 _$ |& D+ t0 g9 A
       - 将原问题分解为更小的子问题: M/ B: A" i. e
       - 例如:n! = n × (n-1)!6 _3 b9 [$ P1 d% t6 N: T

    6 @* V3 R; F) t5 ~# g  b1 h 经典示例:计算阶乘4 b4 t9 d, J, ?: G
    python
    ; Q6 y4 @( r# U4 T/ s2 ?def factorial(n):
    ( d  _! P( w- [9 h: G! H6 n% O4 u( h+ |    if n == 0:        # 基线条件: ]" `* v( m( b7 ]) F$ v
            return 1
    3 E/ w, b% z" G; A: g    else:             # 递归条件0 E7 G' _- u. q" @
            return n * factorial(n-1)- X. A  M- }; x" X. G
    执行过程(以计算 3! 为例):
    $ u( ]2 `! _; _  E. A0 lfactorial(3)
    , _8 {) P0 @3 V% \- P3 * factorial(2)
    0 }7 W! z7 F6 v, d3 * (2 * factorial(1))) P: l1 i* c  k1 e0 v
    3 * (2 * (1 * factorial(0)))  ^- R4 p9 h$ ]; o, [$ |
    3 * (2 * (1 * 1)) = 6
    $ Z6 i' ]6 i1 e" a0 e& `7 M' u# \1 F. r) L  ~' V/ L
    递归思维要点
    & u9 ~& p( @8 s( O) [6 V' Q2 i1. **信任递归**:假设子问题已经解决,专注当前层逻辑% `, ^$ s4 `9 P3 D, J" c1 r
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)/ Q0 _: U* e- I: a: S" {7 U
    3. **递推过程**:不断向下分解问题(递)
    $ \9 y( S( _2 Q) _/ i4. **回溯过程**:组合子问题结果返回(归)
    5 i/ V) d( O$ {, g# N4 g5 ]) H8 g( k# G! F) I
    注意事项4 ~) f  V4 w- h1 j& C- ?7 t: p
    必须要有终止条件
    - w3 u, f" z5 K+ {0 d- W递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    / }' _, T  O! w某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " j, \; R& e% W( c0 z5 [+ c尾递归优化可以提升效率(但Python不支持)/ L$ r6 q0 K0 ]2 J* k
    4 P8 i( J0 @5 `6 u
    递归 vs 迭代4 M- g0 z  b8 t7 v6 c
    |          | 递归                          | 迭代               |
    + M9 f# x$ S! e4 k( u|----------|-----------------------------|------------------|  K7 {8 Z4 M4 c7 N, `2 B/ [6 V: a
    | 实现方式    | 函数自调用                        | 循环结构            |( v9 q# ~4 N  s8 w7 ~8 s& a. L
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ; L$ q0 m: r- E& W$ b| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 V  g3 \' n! Y# M0 J| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |, T) E  d5 @+ `

    * p+ {0 M, L" B# b* `4 Y4 R/ z 经典递归应用场景
    ! m) D/ j, ?/ U6 d4 m9 n  d1. 文件系统遍历(目录树结构)
    " `! q4 \1 l% }4 Z! ^9 y; o2. 快速排序/归并排序算法* ?  ~* P" U: i) _* T
    3. 汉诺塔问题
    4 j" q7 H& D* l3 ^$ _) p$ _4. 二叉树遍历(前序/中序/后序)
    - n" s+ J9 t4 \8 S5. 生成所有可能的组合(回溯算法)
    ' F# Y5 x, D+ h5 J) \( u0 a+ [7 H8 _0 h; x8 q: O1 P2 K: W* u4 [9 j
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    8 小时前
  • 签到天数: 2841 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 k# U! \: N0 e3 L; o
    我推理机的核心算法应该是二叉树遍历的变种。3 n- i/ C) b" |1 L
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
      m9 j. Z, p5 z  |7 P5 H5 ~Key Idea of Recursion
    0 g8 u6 y7 K  a; k6 b! w9 f6 t! U* K# d" e. x
    A recursive function solves a problem by:% s/ F& c0 Q% t" d" `

    . l( O, n3 \/ A" f' H) ?    Breaking the problem into smaller instances of the same problem.
    : }. t# p5 v1 b+ V7 }. }0 M' o) X; ~# t
        Solving the smallest instance directly (base case).
    6 V2 q# p) r$ H0 |8 S
    - h8 s+ s+ R% ^  I    Combining the results of smaller instances to solve the larger problem.. s; u, X+ U2 h" }
    ; r9 r9 T- c" {1 b  e0 u, p( g) G
    Components of a Recursive Function$ s7 ?$ l# J  T. I& g6 F. |6 T
    % z$ q  u( Z0 |5 ~/ G
        Base Case:. Q. v4 a: y% E& A2 ?3 z# j" P

    % e5 {% ^. D5 l        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    : t4 g9 m) v0 u# m6 y; m3 j* ]! i, C4 X/ m4 q& ]
            It acts as the stopping condition to prevent infinite recursion., a/ X% R8 [" e" w* l) `( u# H

    ) R. ]* k+ F8 O. L: ^+ V        Example: In calculating the factorial of a number, the base case is factorial(0) = 1., G( M* A; A4 A6 W

    0 h9 A- E4 d0 h7 c% x    Recursive Case:
    . Z6 n' v& i( F! R; y! @, _6 C+ p* w. @$ C0 }6 s  C2 b0 {
            This is where the function calls itself with a smaller or simpler version of the problem.6 f5 m* `6 S# D- S% n( I
    ( G& W5 P& H: r9 k" C8 L
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
      F+ `$ B9 J# x' x2 X$ t4 ^' X4 r; b1 Y" w$ y: _
    Example: Factorial Calculation
    9 b8 U; u$ _4 s
    1 O& _8 u+ H6 E4 \3 z( I' Z$ KThe 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:
    9 w4 w1 e4 r, b: N1 j1 {
    4 A, t. v4 |+ X/ ]7 n6 k0 x    Base case: 0! = 1. I  i- s# F  o, [: d

    ( H: _8 n" \5 T1 P/ F; n    Recursive case: n! = n * (n-1)!
    9 A7 y# F/ R$ U; x9 j
    4 H/ c9 F" {& c* L- qHere’s how it looks in code (Python):
    ( m3 H2 H  h! i( tpython
    $ A, P0 P3 P) M
    * ~# B+ K# Y; B: @3 U0 O
    - y, n4 e( Y! N0 \7 vdef factorial(n):
    , h& Q! M$ r& c8 q    # Base case
    : Q3 \' C3 w3 K    if n == 0:, g% [9 `* I# i
            return 1# Z! P0 n( B9 O3 q5 B4 J
        # Recursive case
    6 \* k, D1 _8 ~+ w9 _    else:
    ' h6 j9 i; E3 ~1 W        return n * factorial(n - 1)0 k) r5 p% a+ t7 ?# S
    4 D* l* y2 q! {+ |% o3 p
    # Example usage2 L/ r) d& M6 n# G
    print(factorial(5))  # Output: 120
    6 b3 g& V! @/ a- I2 T/ g3 y' \! {. U: Y
    + q% d* Q# Z4 j$ V- }How Recursion Works
    5 D8 @. S9 O, i# ^1 v0 m5 s" m: Y0 K0 S- j4 T
        The function keeps calling itself with smaller inputs until it reaches the base case.
    # W* R  B8 G" u$ O4 k4 i$ ^; C' D6 N, [" T" S4 q7 d5 h1 ]- ^) m
        Once the base case is reached, the function starts returning values back up the call stack.
    6 Y& T. \. b3 k& B; M% Y( \
    + v2 Z% J1 V# G9 X    These returned values are combined to produce the final result.
    $ F& u& L- o7 t4 W6 ?
    0 q# N* \! E2 j, A# X9 CFor factorial(5):
    ! W- f7 R; o5 L) s1 f. }
    / D9 _0 [/ C; T( A" L( d/ D) d, z4 [% E$ s
    factorial(5) = 5 * factorial(4)
    ) h( K: r8 b2 w+ tfactorial(4) = 4 * factorial(3)# G" Q  ]% z. D- D% J
    factorial(3) = 3 * factorial(2)! i1 c3 w" D4 K0 w7 V* V
    factorial(2) = 2 * factorial(1)
    , q& c; r& z4 u( g' @$ n! nfactorial(1) = 1 * factorial(0)
    # Q. Z/ t/ Z/ ?: I! xfactorial(0) = 1  # Base case3 l. ]$ X1 W/ i% q$ e  @( U

    - T& Q& F; m& h- H( v9 R8 \Then, the results are combined:
    9 G8 O0 _3 @" F5 `! u+ k" H# h! \4 O; Q% W! F
    7 W& A* h4 w1 D. p+ I$ L9 _
    factorial(1) = 1 * 1 = 1+ Z% W  ]8 ]6 c
    factorial(2) = 2 * 1 = 2
    ( Q1 ^* Q( D/ |. Rfactorial(3) = 3 * 2 = 6+ |( t: W" _0 E  o$ |& J* V2 l
    factorial(4) = 4 * 6 = 24
    % f5 U) {  I) U$ yfactorial(5) = 5 * 24 = 120# e& ?6 A7 c+ Q

    * Y, s, O# K3 t) u; UAdvantages of Recursion- n5 a. K& Q: m1 @

    , {* w( ^$ n0 ~4 e5 p& f! |2 L1 ]3 z    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).
    & T# C7 B. ~3 d* s/ V1 g0 V, Q# H0 x5 U8 d
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ) f% o4 P, U3 e9 o/ S
    + x3 h4 ^. U! W4 M6 W$ B8 UDisadvantages of Recursion
    4 H# s" i" n; b  s7 ]3 u& m  a5 _8 x) I" u6 \2 v! A
        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.) H  J& V  e! E5 \/ l
    ( `4 k6 L, |4 K' ?" J- x  w
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + B' q% V5 _! x  W1 l
    * J( g3 Z9 ?8 V! v8 q# R. r: l5 l4 t0 LWhen to Use Recursion/ x, J( {+ C0 |6 a( [8 C# ^1 R

    ( w# D( @" ^! I0 L4 ^    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ; y4 w0 R( f) {; Q* f8 Q+ ^9 `% _$ B5 Z4 l, a6 z) r" c
        Problems with a clear base case and recursive case.7 [7 \& j4 l* a+ U3 z* n1 |1 m
    5 y5 b. {( D: B" W7 e% {
    Example: Fibonacci Sequence3 Q. w/ i! K, X& z8 p) R
    . q) t. ~, q0 Q3 _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:% u! A) o0 j3 C0 d) S* V

    7 k( ^* A1 ^4 i0 G    Base case: fib(0) = 0, fib(1) = 1
    - D, R$ a' X2 h& Q0 J" P! g1 m/ `9 c4 y- P, X) m9 C# U& `( Y
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ u- I+ L2 w7 F+ y# I! E
    $ i( r( e$ D3 u( H- I& y* G
    python7 M. |' s3 Y) S2 A

    / C2 Z4 S# l* z  M2 n; w6 E6 @
    + f2 D% C; k) u6 e) Idef fibonacci(n):) l6 Z2 @5 C9 Z3 @2 h6 @5 p
        # Base cases
    : s9 J  D/ t7 R, I8 g3 q    if n == 0:2 r8 |. {2 q. g4 r( ~# m
            return 0
    6 I  W" ^/ a% d5 v& n1 u    elif n == 1:) |0 s7 o5 S. z2 [6 n) _
            return 14 Z7 e  z: P# o% Z4 s
        # Recursive case1 i$ Q+ L- N5 ?- h' v
        else:
      E5 C( k# P7 F  l" h* q; D        return fibonacci(n - 1) + fibonacci(n - 2)- A; b1 |9 K+ \) c! o, G

    3 U& ?' R; E) O/ Y% g& N$ P# Example usage
      u, \+ t0 X6 F& z8 Nprint(fibonacci(6))  # Output: 8
    . q6 O/ P# N0 ?
    6 j  Y3 w! |6 t% r6 O& yTail Recursion, {2 u2 c& t$ z* t  L
    3 N" _4 J/ g! ~
    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).* J& J9 X7 V, j: Z
    , }6 T# j( o! N: l9 N# e
    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-2-23 14:11 , Processed in 0.035078 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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