设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & C8 n* O( D9 \7 U3 Z! q5 Y/ O! y7 \" ^" m' J& ]4 P9 H2 ^) @2 R
    解释的不错
    " O1 y5 Z  I' j
    ) G/ F& R9 u$ [5 F递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。4 K/ z" ?; l; s. b

    % F* @8 v+ w' m. p& } 关键要素% D4 V4 g/ T+ l. l, m! e  Q" f) ^
    1. **基线条件(Base Case)**; M2 B, g& E, I/ }, v
       - 递归终止的条件,防止无限循环6 w3 d: {# J$ w
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% J" i8 D0 B* _; @+ V6 Z. u# X

    9 b) x6 U/ y% y& d5 Z' y; \7 u2. **递归条件(Recursive Case)**
    ; `" n+ h+ Q4 Y/ }4 I2 q   - 将原问题分解为更小的子问题! k" N# Q( }4 Z. {& K2 a# K
       - 例如:n! = n × (n-1)!
    & A  Q1 o) U  B: C! O" c3 O7 F
    ( _7 `5 ?7 Y7 ~2 n1 j5 g 经典示例:计算阶乘
    % m# @9 J) W  k$ ~9 vpython( j, s  v" S* q7 A- W1 G1 ?
    def factorial(n):2 C6 x/ j- N! p1 Z7 l( C: k
        if n == 0:        # 基线条件
    + {, B8 e+ J+ ^7 U        return 1/ G, t7 e6 ^! W: [! p
        else:             # 递归条件' ?" |6 j  y, B8 D* X0 V% B
            return n * factorial(n-1)+ G/ z) t: ?2 R; }; a4 {* h+ V- w
    执行过程(以计算 3! 为例):! I: u4 Q9 H" ]0 v
    factorial(3)# e: y( Y7 y6 A* X* U) J' `
    3 * factorial(2), x9 t$ V! u6 Z# r2 F7 Z  c/ p
    3 * (2 * factorial(1))
    + R& y; l4 W, W7 C  M+ o1 @3 * (2 * (1 * factorial(0)))
    - u) Q9 {5 _; k; ^# K: o5 [3 * (2 * (1 * 1)) = 6# g& S' r3 p5 O& K* \& r1 @

    2 s# ^; N( G9 d4 m  v; @$ g6 P% p 递归思维要点$ N" m/ @' M" s- r1 `
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑3 Y0 W3 }! D' v+ q- p
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 [) b2 W4 {3 l4 |- p9 ~
    3. **递推过程**:不断向下分解问题(递)( a& C: {" B1 P
    4. **回溯过程**:组合子问题结果返回(归)
    . j8 m. l& p9 P& L1 g8 Q) @1 r) P' `  m* U' w3 ~6 ^- U
    注意事项
    & {; }0 F. C1 z4 h8 Z+ Z! ]+ j9 @必须要有终止条件
    8 A5 N9 X8 Q+ V% z: c递归深度过大可能导致栈溢出(Python默认递归深度约1000层)( F: o4 M% ~2 T
    某些问题用递归更直观(如树遍历),但效率可能不如迭代2 v' e; @! A3 V, t  G1 o) |
    尾递归优化可以提升效率(但Python不支持)
    , V7 g9 m. e' Q9 |2 R. M4 h2 G, V+ V7 m$ e: t
    递归 vs 迭代) B5 |. L7 V" B) s" B
    |          | 递归                          | 迭代               |
    : Z& z; m6 S# O8 X% I|----------|-----------------------------|------------------|
    ) }8 K, d7 w- C) c* n4 u| 实现方式    | 函数自调用                        | 循环结构            |
    : Y0 S! R/ Q/ p# ]( M| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ N7 J# o" A3 a$ w
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    . Z- h( a" V1 e! k6 X/ A7 t| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    $ P6 {% b1 J4 N
    ! [! V1 }7 V4 Y 经典递归应用场景; H+ W( {- Y7 W7 p7 r  @$ E
    1. 文件系统遍历(目录树结构)
    % Y. U8 V8 w& A9 `. p2. 快速排序/归并排序算法
    ( F7 _! H- }# {9 L3 E2 C) J3. 汉诺塔问题
    ! X5 Z  P0 \- Y3 R8 j( H7 [4. 二叉树遍历(前序/中序/后序)% q" o# g3 X$ [: t' z' x
    5. 生成所有可能的组合(回溯算法)" f6 `+ B. [% z! a; x( ~0 P
    / H4 d4 k& m) G1 [% o
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    前天 07:45
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    & f4 R6 Q# h9 i% I我推理机的核心算法应该是二叉树遍历的变种。
    1 `5 W+ Y  n- v* ]8 G+ j另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:. I& V. W1 J( w" }8 i7 m$ A  @  m
    Key Idea of Recursion) g) o5 k- Q2 O# i' j7 G. C
    % D& M  c' A# ^9 [, c! F6 T
    A recursive function solves a problem by:  X" D* Z3 w, l# x

    7 `( g* t: }. V1 q4 Y    Breaking the problem into smaller instances of the same problem.0 @' V# [/ K, f+ Z$ V0 M5 w, Z
    7 I" I2 n! c+ r9 W
        Solving the smallest instance directly (base case).7 y4 C  [: v) d4 s0 b
    & ?$ {. e) \! T$ ?; t4 {4 H0 C1 u
        Combining the results of smaller instances to solve the larger problem.
    + n0 A" r9 l5 Q$ b* L$ _: F9 I) v: [* g* g. J2 x* r" E8 T  y
    Components of a Recursive Function
    6 D& C+ }: J/ b* n  c  \
    $ n$ o% }* c0 L3 D    Base Case:
    # v9 C( T, p0 ?4 \* `$ C/ W
    * V) @& s- N. f( O) o; [2 ^        This is the simplest, smallest instance of the problem that can be solved directly without further recursion./ Z- f6 W# ^% K( h
    9 A/ Q) J' ^, s# g+ d2 n+ [
            It acts as the stopping condition to prevent infinite recursion." ?1 V# H: V/ X5 @3 H& ~& I" [

    ! g  }2 \4 c$ c0 @# S) }/ l8 s        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.- I+ \: T; d, }7 d6 h

    # k! Y) h6 c  Y2 c4 t5 h; P/ m    Recursive Case:
    ; e7 D( [  b  E0 n, H( m  z* X4 @) R" X. M
            This is where the function calls itself with a smaller or simpler version of the problem.
    - [; ^. h; k' \* C4 S3 h4 g
    % W1 _0 i1 L: ?/ u. A, M        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    - W; ~( y& ]  f5 J) x2 I$ a5 O  g4 m& h4 \) J1 e+ Z
    Example: Factorial Calculation3 w+ `( }: `2 r9 {2 a! Q
    $ k, o  p8 z& [0 T/ I
    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:) J3 O7 Z1 A) t# ]
    , w* i7 U" @2 E$ m) [
        Base case: 0! = 1
    7 A% H( c& a* K6 H. s5 Z4 M, G  E6 r" ^3 d% W& s
        Recursive case: n! = n * (n-1)!4 Y) A+ ]  C3 C7 w7 \, ^' a
    $ Q7 I7 n" ?; v6 C5 ]
    Here’s how it looks in code (Python):
    ( g0 F) l$ q$ f" l& Kpython
    ) B' a+ i- `9 M- Z( b1 u- y8 v, c5 R+ G, l2 b, d3 _
    9 @* L9 `( G' z: o$ U
    def factorial(n):
    - h0 g  t, N( }( Z: B    # Base case
    ; M! |4 O' {5 J- G1 q& `    if n == 0:2 N1 y- r) A7 Y9 `  _  M9 O
            return 1
    ! I$ n/ P8 w3 ^3 F1 ]    # Recursive case
    ( I: z' h: m" N$ e4 S; m3 a7 h    else:
    / B- _# g0 Q0 o6 U) q) o9 d        return n * factorial(n - 1)
    . a- x& S* t# V9 i& ^4 Y3 z; O; `' P. I
    # Example usage
    0 e& C$ b7 j4 M+ e! A# bprint(factorial(5))  # Output: 120
    " W0 y( E1 J; e; \( z$ m. p" W7 d. [4 M
    How Recursion Works
    ( C1 ^  Q$ s- E+ @% ~, C$ m/ m
    ) n# S9 R% c8 l  O4 A; [8 o    The function keeps calling itself with smaller inputs until it reaches the base case.- z" y8 [( x  \  U5 k7 l- n
    , g; {8 f4 W$ J9 `4 ~8 s2 E
        Once the base case is reached, the function starts returning values back up the call stack.
    . X1 _+ m7 }8 R$ x5 f4 ]2 i4 C0 k7 B, X, X
        These returned values are combined to produce the final result./ u2 ~1 B6 o- D! q* \
    $ Z/ y7 N7 i& @# O
    For factorial(5):  `% p3 @: [9 w5 C  B: a
    / Q/ u6 p$ M) ^* A2 g: F; K
    : |3 H3 B7 L9 y1 m1 k' J
    factorial(5) = 5 * factorial(4)# z) A# G$ U+ u. i7 H' Z) ~
    factorial(4) = 4 * factorial(3)
    3 y* R8 x* Q. ?9 ~) Ifactorial(3) = 3 * factorial(2)
      {# h# c! x  w8 {0 ~factorial(2) = 2 * factorial(1)
    ) U: {  _+ m: ~' O) n; \" Bfactorial(1) = 1 * factorial(0)5 Z, q, t, z9 U9 z3 u
    factorial(0) = 1  # Base case4 b. n# }% H1 T  W. _9 g" Y

    " v  j% X5 ]! D0 {Then, the results are combined:
    : {+ t+ N$ _6 H# u5 m7 P, J* q& c' ~7 m& a! I- `
    7 v' U/ s( w) S( {2 E  j- O
    factorial(1) = 1 * 1 = 12 {( P1 e& r( A: Q/ Y4 n
    factorial(2) = 2 * 1 = 2
    9 l: O& M3 B6 T2 Z0 g3 Ufactorial(3) = 3 * 2 = 6
    + t3 I, R$ n4 F% q, o3 G  tfactorial(4) = 4 * 6 = 24' a/ C' Q" Z- S! N& P, ?
    factorial(5) = 5 * 24 = 120
    8 I& _. u1 [% `1 L4 ~1 U* b
    0 }2 ?$ A: f: |" \0 l* S) ZAdvantages of Recursion
    0 F( R4 r0 b. A, \( q0 ]/ k8 G4 _, H
    ! z$ L6 S9 h" I$ E3 K- f; G; h    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 b) e- U3 @5 ~+ ~' p0 M
      U  \- x0 I$ P' Z4 h# B  n
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    # P' j; l; Z) }' J/ n2 Z/ F3 P2 [! Z7 P6 v
    Disadvantages of Recursion  Q% a( A7 e1 A" P5 \
    4 ^+ k7 @' p2 Q) ]' z9 Q( f" A! Z
        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." C' g! z/ P, K8 R2 O1 q) {4 ^3 e
      B2 o2 q' D# x) }: s
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 I9 e' d' F" W# A8 H3 c
    . a3 U8 c  w3 F' s' w0 }/ ~& H, p
    When to Use Recursion: Y* L+ Y2 s& d. P6 H7 I
    $ {* ]( P1 r- x; D5 K
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    5 c8 `5 [% S5 r+ x) A
    . B* Q- N) X4 w    Problems with a clear base case and recursive case.
    * @0 M4 Y. H  K% w) Y2 [9 W& B/ j, e4 X1 V( u, I% s
    Example: Fibonacci Sequence
    0 c9 t* I4 ~; B# i) g! `, z; ^& q$ b- g. p- u& y4 s
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. Y* `& }7 |9 M
      F7 R, \3 q( D# e( Z. L9 w
        Base case: fib(0) = 0, fib(1) = 1
    - U* o! {: s; o1 T6 I
    9 r# t. ~) }  y: Q! T    Recursive case: fib(n) = fib(n-1) + fib(n-2)8 F, Q' B: C3 N

    2 f2 S! b* v* O1 P- W8 x$ {python
    ) C+ B7 S& D# P. _. @: \4 N
    ! B1 e7 D, C' g1 V' }5 T2 `+ k6 p8 x, Y* u
    def fibonacci(n):+ Q2 S' r* ]& H5 u9 H- c# q
        # Base cases
    ) K# B' f. W2 v9 f$ p# t    if n == 0:
    $ M8 f. ^" ~! z$ i9 u5 p- n        return 0
    3 ~* d; ]' G& r, q    elif n == 1:4 T$ h) y. d$ k
            return 1! W* z% j3 O3 w8 H
        # Recursive case( b3 I$ K; j* M( W6 Z7 L. P
        else:' _: X  o+ J8 k* c# x
            return fibonacci(n - 1) + fibonacci(n - 2)
    2 [+ O4 ^( Q% Q  e0 Y+ a8 N! Q/ J; X2 F' K+ X: a1 u
    # Example usage" E) J# r6 w4 H. ?8 P$ V6 v
    print(fibonacci(6))  # Output: 82 A' j; g7 l# K) t
    : h0 i$ ^" d6 n3 h5 z5 Q: u" M
    Tail Recursion# j( }4 @( J  h

    2 j( d  X9 z, a, t0 a* k1 JTail 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 ]1 A3 m0 Q! G4 T
    & P/ C2 u8 @: M% \, yIn 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-4-11 00:21 , Processed in 0.057287 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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