设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 p/ z$ s0 p$ ]
    & v  E/ u& a, V. E9 m
    解释的不错
    ( [# _- o: j8 X/ N3 V
    / A5 ~/ B  M1 b" T4 P7 X' }; i递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  @. D7 p9 E2 ?4 U5 Y( r/ n

      O: B9 w. Y) S+ d3 H4 }3 G0 T+ I5 ? 关键要素0 h/ D  W. l) N' S, `" B
    1. **基线条件(Base Case)**# V' V1 `, x! L& F
       - 递归终止的条件,防止无限循环
    * P, y/ }# E- d' ~/ t3 G, y9 }* k   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ; o3 l7 O. s8 J2 e' M% Q2 \2 @" g  {
    2. **递归条件(Recursive Case)**
    0 [2 C9 h" m1 A/ V   - 将原问题分解为更小的子问题7 i% q9 I: N: G) `) c0 u0 x0 u
       - 例如:n! = n × (n-1)!
    3 K9 g. v8 E9 y8 J5 e8 ~, @8 d! V
    * G3 Z" t/ p. F) {/ j 经典示例:计算阶乘$ N9 `/ K! N" b& U  ]7 a6 Y
    python
      V& ~+ ]! n' Idef factorial(n):. s5 M$ G' ^- ]
        if n == 0:        # 基线条件
    & v, L# J2 F$ D4 u- T5 R        return 1
    $ n+ B+ I3 P; _  ]    else:             # 递归条件
    $ o; ]7 @+ ?0 O; t5 ^; |& ^- d- y        return n * factorial(n-1)
    : j- ^' _* u7 P1 k# P执行过程(以计算 3! 为例):0 T  `) S0 \3 p6 x
    factorial(3)
    5 g$ y) m4 c3 j  m: m+ V3 * factorial(2)
    % U" Z2 B. x+ b, I/ g; Y) L2 p% k3 * (2 * factorial(1))
    1 w  s  ]- V: O/ W( R& \0 [3 * (2 * (1 * factorial(0)))
    ! r( Q0 r2 F1 p2 ?; {& Y3 * (2 * (1 * 1)) = 6
    1 i- n" y  z4 @1 H# t) S0 d$ H; D8 u. j( X6 K# l& f# `
    递归思维要点5 t; L7 \  o+ S2 d! `
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : r. P( g9 _  V) s0 k2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* W) a3 ?" f" r; P9 Y
    3. **递推过程**:不断向下分解问题(递)- v) f$ a- {  |3 T8 d! K
    4. **回溯过程**:组合子问题结果返回(归)
    , ]$ k" n& ~) g$ x+ `4 `1 e% Z2 Y& y* F9 k4 z, ]7 I0 R. M
    注意事项* G5 P% O: w1 w3 C$ D
    必须要有终止条件
    # b8 F: @! \5 |% w& {* E递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( q3 T# q8 @1 B) m/ j( e某些问题用递归更直观(如树遍历),但效率可能不如迭代+ z! c& R. w- h- {0 |8 N
    尾递归优化可以提升效率(但Python不支持)5 K! E# \& U4 n
    9 Z; ^: I% I: |- ]
    递归 vs 迭代# k1 B5 D" l2 d  k4 W
    |          | 递归                          | 迭代               |
    : P! }6 ^" b4 K0 M8 ~% T|----------|-----------------------------|------------------|8 u9 L2 ]$ K4 P3 h/ i7 G
    | 实现方式    | 函数自调用                        | 循环结构            |
    4 \; [, v4 y5 {- V| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |" Y/ C0 C* y2 v' G" O. e
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    3 t2 J' r, M5 y, X$ _8 E| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% E. X0 l; h& o3 a
    0 w6 ?2 P4 _, {# \
    经典递归应用场景
    " s, {1 ?( a5 ~4 l1. 文件系统遍历(目录树结构)- s& J' t- Q# a! w
    2. 快速排序/归并排序算法
    5 K6 _! Y' O# \, K& O3. 汉诺塔问题5 [2 s- n/ Z" _! Y6 T
    4. 二叉树遍历(前序/中序/后序)& R% q6 Y. r* G2 U7 c1 I& q- h
    5. 生成所有可能的组合(回溯算法)
    1 K1 A& s: A8 H8 }8 F3 x0 Y0 K
    2 F% O  G1 K3 b+ z, D) G试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 07:05
  • 签到天数: 3143 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,9 R7 o) p- a. [3 k0 u& E% G2 f: v9 I
    我推理机的核心算法应该是二叉树遍历的变种。
    ' |: r/ i) }( M* n另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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' r6 L$ F- s. fKey Idea of Recursion
    9 j9 E# O0 m6 a
    0 A  f- K0 S: I: u3 B% w7 JA recursive function solves a problem by:
    : s! \/ J9 y# c
    ; n7 |. Z6 f, f+ L' m    Breaking the problem into smaller instances of the same problem.* N+ Y0 Y$ [5 t: D1 b5 y
      |9 a# B; X: O& {
        Solving the smallest instance directly (base case).7 t: s3 s9 @2 N. L6 m6 t
    * }# a! ^1 E7 L0 a5 e7 W
        Combining the results of smaller instances to solve the larger problem.* }; u9 Z9 {+ c: Q' b

    % A5 f2 N1 r; ]$ |Components of a Recursive Function& @! Z( b5 p% Y& m. H

    ' m) S2 x" ^; v8 l5 f8 V6 T    Base Case:
    8 ~2 B8 K! z% V5 h% ]' ?; d! y/ J( g; S& W- @+ c% J& X* y1 u0 D
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / @+ l  X: B/ ]$ x
    3 a8 x& m- N& }7 d/ F3 l! _        It acts as the stopping condition to prevent infinite recursion.  u$ I1 ?8 i& g' t  d

    8 x9 h. B  p6 b: d) j/ c1 H  _        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  U' U" ]5 A" s  o8 j. I

    ) r9 ]4 i8 j' m$ c7 v1 W! j    Recursive Case:
    $ O' @2 Q1 u3 H2 i( K7 C; e9 h0 S% |6 o9 A
            This is where the function calls itself with a smaller or simpler version of the problem.
    4 R. q) j' B6 T7 z+ g
    % X3 G# r  |" h2 F; v) {' J        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).$ O6 T  Z6 h, ?" T

    1 g4 p6 V/ _, M# ]( P& g/ M4 mExample: Factorial Calculation" B& G  }+ r( ^- t* Y: M4 F) X  }  V3 ]
    ) N  C. @, N0 r2 f2 k8 w
    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:
    " A+ |2 i' G4 }9 f! f/ k; J2 ]
    3 m$ h* r; ]- G  k    Base case: 0! = 1" W* a, ]0 G7 G' a7 e

    - @8 Z/ ^! M' H$ u: M6 ?    Recursive case: n! = n * (n-1)!
    ( x3 S" H) i, w' r/ I$ r0 I
    . ~, y! D/ J5 t# Q. N) dHere’s how it looks in code (Python):1 m: C# O) y' B+ O% W0 ]4 {; x
    python
    - r, {; d8 }: t$ b
    % j" B. E9 m. O: Y- G. ]+ e
    9 m( H8 p$ i9 @def factorial(n):
    " `7 V8 h# i& m" n! m    # Base case
    $ u" {9 ?7 }5 E) R3 l$ U, k    if n == 0:3 y( F! B6 n! r7 r8 N, `# C
            return 1" L" n9 ?( I! E" a% f
        # Recursive case% R7 J) g8 [6 ~1 C: C
        else:, {  Q6 Q0 c0 k7 h
            return n * factorial(n - 1)
    + X' J* b# O& @, i- |1 [8 p' E
    9 Q6 h$ N  l( `  T* R# Example usage
    & h0 W1 {  v: oprint(factorial(5))  # Output: 120
    7 m* `" h, }/ E. l0 g/ v
    9 o- S* t6 |' _1 b2 b. G% FHow Recursion Works+ `0 y5 J1 @" K$ s7 Q

    # ?* b0 Y! i/ ~( h4 x' Q8 \, T1 {    The function keeps calling itself with smaller inputs until it reaches the base case.' Q; j' ]5 w( O9 ~' z. u
    . \6 \' f% B' p4 o! Z" \
        Once the base case is reached, the function starts returning values back up the call stack.
    ! R; O$ X5 a6 Z: r6 t% F$ d$ C% N" g- j. f
        These returned values are combined to produce the final result.
    9 p8 F" X/ ]- M/ Y: a
    ) S7 r4 l; H9 LFor factorial(5):; X  K+ K: [0 i- m) n6 \' i
    ! ?9 {# a3 A; {9 c  l

    ' g' @3 D& v  U0 ]2 l, Nfactorial(5) = 5 * factorial(4)
    1 N( D5 {. G! ?! N  dfactorial(4) = 4 * factorial(3)7 J8 m/ \2 q7 T" I2 V
    factorial(3) = 3 * factorial(2)# Q+ r# H: Q# j/ T1 t/ \( _  B8 g* F
    factorial(2) = 2 * factorial(1)% |0 r$ i' O4 x8 X( K0 f
    factorial(1) = 1 * factorial(0)
    . O8 B7 C  I+ Lfactorial(0) = 1  # Base case
    / x0 p0 b/ o1 d$ B# f
      F: H$ }6 [9 J: rThen, the results are combined:
    ) _5 x" j& @$ @: r! u" D, y5 F+ o: K' O! F- Q/ ]3 O

    ( J; t" K+ [/ J" {5 zfactorial(1) = 1 * 1 = 1
    ) o7 h6 U' Z" T5 W$ v: l: _( s% tfactorial(2) = 2 * 1 = 2' I7 u8 }( |, \- r
    factorial(3) = 3 * 2 = 69 s) Y* A' e6 m
    factorial(4) = 4 * 6 = 24' P+ T1 O$ A8 M% f
    factorial(5) = 5 * 24 = 120
    . G$ A. g5 d5 R" C9 j) V$ I* h" v6 p, j2 N) _7 l. D. s
    Advantages of Recursion" l% W2 Y% C$ h* V
    $ x1 K# Z; {$ \0 K" j
        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).; @. g6 f* b; l- m

    # n+ A5 Z+ p+ y9 e3 o2 e    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    0 x8 e# F! g% g! S5 k/ P
    0 t* `' u) ~* T7 ^  X) U! ?Disadvantages of Recursion
    . C$ O' P5 H" K6 r
    6 P2 D' P1 z, R# 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.
    ( p( |: e7 y2 i. g2 F  d1 ~
    9 b; m- W" h' R% \    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & b6 h) q; m7 [1 W! Y
    , q4 |- M$ s6 m% n# ]2 N; BWhen to Use Recursion
    ; e' r/ V" C2 |; I/ o9 t
    - P  l0 z0 s, U; ?/ [9 ]    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 Y6 N' v7 ~. ]/ _$ Q5 y! n0 A, C( H

      V6 b, `& b9 t    Problems with a clear base case and recursive case.. q* g9 W/ M: F* f% ?+ A- F( ~1 Q! T
    7 f6 T; I" I9 W% O2 `) @8 C8 f
    Example: Fibonacci Sequence
    : [9 b) ?, S5 |+ E1 f3 x
    . c6 M# E+ J, zThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# b5 f# i: b- Z# f- G1 P1 r

    ' A3 [% J6 o) A; Z$ q! l/ M# C, w    Base case: fib(0) = 0, fib(1) = 1
    5 z: e3 ?& o: I6 k3 Y: O  U5 W2 f2 ?
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    - o9 ?3 A) [9 ~, i# a0 ~) S( S, W% b+ @! b# {% @7 k& ?( g: E4 }
    python" P0 L: B2 d* F4 L* q

    ' H6 U0 K* w+ X6 \9 u0 o
    $ c0 x; t. U. ~def fibonacci(n):
    / o" g" a0 ]- `: m1 Y    # Base cases
    ) G, [) r% T7 Q3 T. Y1 c! G3 w    if n == 0:: S6 R6 M/ l! T! k1 Y' l3 o
            return 0
    9 v/ o2 R' g. s+ M& f7 i    elif n == 1:: C9 a0 a/ r( X  @3 ~( o7 O
            return 1
    8 B% R# k7 T: I! ]    # Recursive case) J7 C5 g4 w+ s7 R% h
        else:
    7 N2 z5 ^- g4 s9 [        return fibonacci(n - 1) + fibonacci(n - 2)
    1 ~8 L- _' M5 g2 M' @8 C- f1 v+ K1 B/ U0 u
    # Example usage
    & m& R6 v5 [, ]" W7 r$ f. Hprint(fibonacci(6))  # Output: 8
    , z4 X% y" n  {4 C$ c( _% w, [$ u; k( c6 y6 L9 u( p
    Tail Recursion
    8 @8 _! u% Y& ~" t; T, ?' N7 U" S' D2 D! R! {) \' b) 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).
    % d* C; n5 w/ c7 t" m/ G/ x5 s
    4 u& i' S  o7 A2 J; u. j4 J: e6 LIn 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-1-12 07:06 , Processed in 0.029310 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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