设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 f" `6 _, \+ _9 I" P3 G% V6 v7 A9 Q& u
    / M- R/ k# z& W8 H- n' I2 l解释的不错+ P0 s. D. T% V2 n" ~: i0 v

    ) z, T0 |2 y; C: X5 v递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & S! q7 p! T. y; p9 V7 k. W  P. M
    1 k6 v6 y- n6 p; d: ^) H8 ~9 O 关键要素$ _/ K! W5 K1 M: L
    1. **基线条件(Base Case)**
    5 ?! m4 w, M7 P+ Q   - 递归终止的条件,防止无限循环& x. a. ]; {4 I7 f
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    0 e# g8 A& X2 a: n  `
    5 M+ T; b6 V8 f9 w. Q2. **递归条件(Recursive Case)**
    7 R, ]- h& A) V: o$ ]   - 将原问题分解为更小的子问题, u  G7 N- h6 `' M! Q2 d8 H3 q
       - 例如:n! = n × (n-1)!
    0 _6 F2 z  Y: X4 `' _0 c9 t% [& L1 t( A% E2 V
    经典示例:计算阶乘  R0 I0 }& o% ^8 g& z7 b, u9 C
    python
    ( K2 i0 g: u) ldef factorial(n):# P. i: V+ @3 h3 m
        if n == 0:        # 基线条件
    # ?/ K  p( j+ v' @, k1 K9 j" n* p        return 1
    9 s# r% J; u- G    else:             # 递归条件
    : t: F* Y9 E" Y, \/ S        return n * factorial(n-1)0 O. @* T! |# P( q1 A
    执行过程(以计算 3! 为例):9 R4 \, X, T2 r5 N8 p
    factorial(3)
    ( v4 J) ^- P/ M6 ~% h3 * factorial(2). L9 I4 E0 ^3 u- A: W( f
    3 * (2 * factorial(1))
    ! j$ B' q" ?8 q) F( X$ L; F3 * (2 * (1 * factorial(0)))
    3 O$ u) ?: r' ]7 A* K3 * (2 * (1 * 1)) = 6
    $ d% r, I0 m! Z( z" c
    ) M& D& ]( I7 v4 D: j4 [ 递归思维要点" x! w0 q8 B5 ]: F9 P# t4 A
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑# l7 f6 @( F( o* Q5 w% g
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)7 q. v# E( a) d
    3. **递推过程**:不断向下分解问题(递)
    ( W; a0 c( I1 k# s* }' v- g8 N& @4. **回溯过程**:组合子问题结果返回(归)( x9 u( h  Z3 Y" M3 d
    1 \9 Z1 {; r% Q# S' `# d# A0 s$ t
    注意事项4 @, n" s) k3 y" E1 a
    必须要有终止条件$ a' R1 z) [) f8 @. n/ f
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    3 `9 z' O' y. f某些问题用递归更直观(如树遍历),但效率可能不如迭代
    . Y) r8 V/ ~  w6 V5 Y6 o尾递归优化可以提升效率(但Python不支持); X; Y- R5 c4 H. P
    * j* _" G1 N4 N. ^; ~
    递归 vs 迭代
    ' u9 d5 A, p: K|          | 递归                          | 迭代               |) V, H5 P. y: p
    |----------|-----------------------------|------------------|
    5 X. H. g! _9 I$ Y9 `. v| 实现方式    | 函数自调用                        | 循环结构            |
    & D# f/ N( [4 a. I| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |! X- q$ w2 O5 P% q8 Q! V
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ m( E6 A) W* F
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # n1 s# r0 d' R1 r/ k
    , ?4 o  `" j; P9 q, p2 p 经典递归应用场景8 j# E/ `6 M) h' L. r
    1. 文件系统遍历(目录树结构)7 s' @5 q0 Y! D" h
    2. 快速排序/归并排序算法: t1 v- i) I. b( H6 |% u( U
    3. 汉诺塔问题/ S5 k7 Z; m7 A4 D5 z
    4. 二叉树遍历(前序/中序/后序)
    5 {" L( Q0 v2 s$ W: k) D( @/ ~5. 生成所有可能的组合(回溯算法)
    & ], S2 E4 D, \' I3 ^6 ?
    * q. F+ w% s; c$ y试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ) x5 M6 `2 M' q8 b- `7 L我推理机的核心算法应该是二叉树遍历的变种。
    : @- ?; J, B# b% {* [3 G5 o' l# 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:( N& a- v0 |. A. A4 [# M
    Key Idea of Recursion
    , S8 J1 ~1 c0 E. M+ G0 R* a# Z' `5 f8 `& L. |5 }' f
    A recursive function solves a problem by:% a; @; k3 U  u9 {1 W
    6 @3 ^1 d# O, A/ F- D. j" C" b. h
        Breaking the problem into smaller instances of the same problem.4 Y: ~/ A+ d& J7 r
    , u; l1 K; z/ w* o' b: F8 I
        Solving the smallest instance directly (base case).6 B' b! r6 {; s5 `8 [4 o  H
    ' R4 q# m. @* ]0 v) r
        Combining the results of smaller instances to solve the larger problem.
    6 T4 Y' m) O+ f' s9 V8 h8 J1 h1 `8 b& D* R
    Components of a Recursive Function
    4 Q% \2 F7 k9 Q& ]+ {' T5 Q: Z+ i1 P3 d
        Base Case:8 b/ F, q' J2 F$ I5 I  j! P2 z

    $ a) o6 S8 |2 [        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 j, `. i& q$ n! ^8 U% s+ ]: k, S
    9 H1 L* P8 h& S8 q6 c# B% M        It acts as the stopping condition to prevent infinite recursion.
    $ D/ F1 F+ Q' V1 D9 Z2 R
    - D" N, q' @+ M        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.$ Z) h! o% T. b: C; c+ ~  `
    " b" \$ E% h% O) G/ B5 D$ W
        Recursive Case:% i* W& Y# ~+ I. S2 |

    3 [# q7 L; C+ q        This is where the function calls itself with a smaller or simpler version of the problem.2 k$ t- m! p& q: C* O

    / W4 n/ f' W7 W8 E' w( @        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
      `4 H  a- a  r4 _
    ' t7 m- D+ |9 V* E% a8 B5 U+ S: cExample: Factorial Calculation- L4 M; E  k) Q) ~% M

    : O5 ]3 H9 R3 ?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:& V! {/ a# N8 x# u) ?9 D, h

    3 c/ G( |% n0 p5 c- G$ p; W5 c1 Y    Base case: 0! = 1
    6 _* L  o2 ^: v2 q4 n
    6 ?$ F4 e; `; }( G    Recursive case: n! = n * (n-1)!1 x/ ~( e& @2 M6 @5 x
    " v3 i7 d4 f! X4 D1 L7 B
    Here’s how it looks in code (Python):
    : S; D5 c! X+ ~; x4 [  D2 k+ opython
    6 o1 A0 E& |4 i: T  u& F& v# s/ W, Z& O" i- ^4 H* u" ]
    5 g+ M3 S1 H* f( A
    def factorial(n):/ Y8 ^. n3 f) I+ J! k
        # Base case3 ?: u) H: t# o' N& @% t# O/ R$ d
        if n == 0:
    * O, P# C% c% j0 B# E# ^        return 1* s4 W) X- V: T0 b( E
        # Recursive case
    8 o$ e& `! m8 o( k2 z* i4 v    else:
      h" Z  M3 L1 _0 q0 X6 b) O1 T        return n * factorial(n - 1)( d* r- L: v2 }( M6 ]$ X
    - e0 j% Z& J% x' W1 |8 \+ `( X
    # Example usage
    - s: P6 f) a( j) I$ N' Aprint(factorial(5))  # Output: 120
    1 s& N7 n: {: z: M, g
    * A$ Q3 M& V5 DHow Recursion Works
    3 ]. a. w, W0 H0 r  E% t: k4 p
    ! C6 h# s1 ?8 w7 j. F    The function keeps calling itself with smaller inputs until it reaches the base case.
    ! g9 [5 X8 u+ ^( ?/ U* ~  x8 v. x: @0 c$ ]
        Once the base case is reached, the function starts returning values back up the call stack.7 N! s# c  X( d& K) l
    0 h5 N" ^$ Q9 R$ C- u3 J+ S+ K# y8 N
        These returned values are combined to produce the final result.
    # m+ h1 Q. E( G1 T  L# U" N
    8 s( F1 ]/ n3 lFor factorial(5):
    4 K% e1 g( }  o7 E+ X
    ' Z3 m5 B; s( {! q( d; M$ {, o. c( |
    factorial(5) = 5 * factorial(4)1 _: l& o" E7 H
    factorial(4) = 4 * factorial(3)3 u- m& w9 {2 {
    factorial(3) = 3 * factorial(2)
    + p3 o  T- B' j6 w, P: Yfactorial(2) = 2 * factorial(1)# y/ o$ V7 R, t2 t7 w" Z
    factorial(1) = 1 * factorial(0)
    ; ]6 D- l! z( H2 Hfactorial(0) = 1  # Base case, B! |5 q& {( F2 U
    - q  {* t: ~1 r+ E4 i3 Y9 G
    Then, the results are combined:
    / w5 m$ {$ R) |0 m
    # r+ m6 V) n3 p" _: T9 D0 e
    / N, o, J2 u4 l: R3 t' Vfactorial(1) = 1 * 1 = 1. M7 R# m; \% k" M4 K. P
    factorial(2) = 2 * 1 = 24 _, w: ^3 i3 x' h. h0 l
    factorial(3) = 3 * 2 = 6
    1 A4 B9 [9 j& V0 X: H. t! h, Hfactorial(4) = 4 * 6 = 24
    / X$ h2 O! ]6 x: G4 G& T0 w5 bfactorial(5) = 5 * 24 = 120
    * l, T7 L/ }/ r3 D7 b4 s4 R( q# J7 `. ^& S3 g' n
    Advantages of Recursion
    % N* g/ ~9 o' b
    * u: Z* ~7 T3 ?/ W8 A- 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).
    8 ?( u  G) V$ j( \% h
    6 Y7 ^) F' s1 y3 Y5 j$ L    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 R+ c" y$ k! p' v  H5 f& T. h# ]4 o; B2 u* e$ }2 m* Z
    Disadvantages of Recursion; o6 e, s9 s, d$ e

    , J7 T; P2 f) u; h$ ^    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.: ]8 M5 g9 `. I0 r" H" _# Y8 L. v
    4 [, [2 t( B& F' d) q; c5 U# v
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - \; d  I7 G& W! v  Q0 O+ \+ ?7 ]
    0 ~7 z1 M2 U  t3 l5 p7 ^When to Use Recursion
    & O+ ]& i; y1 O) q1 V# K+ g1 X; O
    , D- t" H/ }+ E2 Z( o8 C    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ( u% x: \2 O+ R! \8 j0 ]( d5 x+ |" c2 E, M, t
        Problems with a clear base case and recursive case.6 D; X) W6 X) @! |# G
    / P/ d# [3 T; u, f+ u
    Example: Fibonacci Sequence& @* a6 C" K0 {1 t& Q

    / Z1 t" F( Z4 F  k9 OThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 a, K. D' m8 {: F% p
    4 Q, ~6 U! |- u! E. w) b
        Base case: fib(0) = 0, fib(1) = 1
    7 [: P1 {( F3 W3 q6 v+ ~4 `: g- {; e/ U7 a) |9 {. N- b4 Q
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    & r% d0 b9 m% |! f/ Y
    / v$ W$ R( |! W3 k! g7 C) dpython
    % O! m) J4 d, |0 ^  x& D9 A- O; H

    1 Q: z: _  S. f3 y5 _' ]3 wdef fibonacci(n):
      X0 T6 L+ z- e& R6 x# Y5 h8 e    # Base cases, [, u8 n* P) x
        if n == 0:; N. D  @9 j, d& p8 A* x! ]
            return 0* Y, n' D) P+ L: m& e: h* K
        elif n == 1:: q5 f* o6 q9 E; F2 ~
            return 1
    + I4 R- e4 T1 x% M9 Z# ~    # Recursive case
    4 O9 |: S8 B9 W$ v8 y    else:
      t1 u% x& D: C6 |# m/ z        return fibonacci(n - 1) + fibonacci(n - 2)
    + p4 }$ j0 ]- H* }4 U& P2 T
    2 w0 \: N. K4 d" g' m# Example usage
    & A. D" ^& }- z. U  ?print(fibonacci(6))  # Output: 87 p0 q. z! t& R# p& O$ j# g

    9 r( j% A+ X/ O( D0 j- NTail Recursion+ m' C9 z8 T9 U, |2 `! Q! E4 D
    ' ^7 j% i! H3 L. \. _- c& h
    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).# Y8 D" {) L1 D9 m
    ( S3 ^0 b' Q$ s, W
    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-7 11:22 , Processed in 0.030421 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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