设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    $ c4 m1 V% Y% U
    3 W6 F4 I3 h* P4 J解释的不错6 H2 N$ }1 p* b* w0 r
    1 @) @! C. @& |& s
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 ?9 }# Z: R% U
    ! A$ p7 P; x1 `! K* a6 V( y/ k 关键要素; c2 r7 y3 ?4 b- ], _% n
    1. **基线条件(Base Case)**
    ( O2 |3 f; K4 w   - 递归终止的条件,防止无限循环  {; U' `* n3 y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    5 E3 ?% ?" E  s+ c0 {# J. u' s% |$ F& h0 N& U' \6 ?$ v
    2. **递归条件(Recursive Case)**& ]6 T) z8 T& x* C
       - 将原问题分解为更小的子问题
    2 {8 P, i' ^" k: I   - 例如:n! = n × (n-1)!7 V/ J/ e9 b$ B$ I, S, b

    + @2 l6 H! U: j, D; x& ` 经典示例:计算阶乘. g; j/ L0 S% Q) L6 q) W
    python0 j2 Q; h% j# _. `0 Z* f
    def factorial(n):/ u% |# G% q' Y  I( I! G0 `
        if n == 0:        # 基线条件
    : ]" f+ L4 N  t# I        return 13 l: ]" G& K' E9 y
        else:             # 递归条件# k& d# W: r' r
            return n * factorial(n-1)# p5 x% L% W" B2 y- j( C$ l% Z
    执行过程(以计算 3! 为例):& t$ k! q5 H5 J  J# W/ `: x
    factorial(3)
    6 g) g- \! o6 T, Y6 f1 r* g3 * factorial(2)& f: {. {! P* s- f. p3 B/ V
    3 * (2 * factorial(1))/ f( y5 V! t, h- v( K; [/ B7 Z0 J% |
    3 * (2 * (1 * factorial(0)))
    ( f1 A8 h# @- ]  i/ Y6 Y( F+ S3 * (2 * (1 * 1)) = 6' R$ j! F* Q% Z4 C5 W4 n) o; X
    ' Y# O! A, H% Y+ N" Q/ j) k
    递归思维要点$ [2 L2 \9 {$ \3 q- u$ [
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # O; M) s# u* g: K+ G2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" R1 a7 a* `7 Q" Y% p1 g
    3. **递推过程**:不断向下分解问题(递)7 a4 G8 y1 {0 J. w  A, D/ `
    4. **回溯过程**:组合子问题结果返回(归)2 y# a6 r8 t$ e/ P8 U

    # m- ^% B% b  p6 t0 L( I 注意事项
    * {7 w( H* S! e6 f' n2 q, h. T必须要有终止条件  j" K/ c0 ^0 l9 ]& H2 R4 `" z
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    0 c1 K1 d0 k; K某些问题用递归更直观(如树遍历),但效率可能不如迭代9 d8 V7 f4 s: c; K" L' t$ g
    尾递归优化可以提升效率(但Python不支持)% P+ _8 x+ _+ _  W) R: i
    - j/ z8 T# y! C6 t3 y
    递归 vs 迭代
    ; n' a0 d, y9 C: \$ G. ^|          | 递归                          | 迭代               |0 V# T  K" o5 L% e3 r
    |----------|-----------------------------|------------------|. _6 h6 k3 ?9 G- u9 h# _
    | 实现方式    | 函数自调用                        | 循环结构            |
    # ]0 q! x9 ~* v) D, H| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , ]1 |- W/ e: }* I4 h' B0 y| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |! g" H+ _/ ~3 \, B$ [7 d. K' Q
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) b) t% o( O: v( V

    $ O/ k( N) M  U, X0 v 经典递归应用场景
    & P! K4 @$ O9 H/ n+ w9 {1. 文件系统遍历(目录树结构)
    ' S$ ^. `! u0 T% [6 F* I2. 快速排序/归并排序算法
    . Q) y1 p; F7 F3. 汉诺塔问题
    # I$ U  z; Q/ f- j2 ^! Z4. 二叉树遍历(前序/中序/后序)
    + q6 t; s3 s& e" v- f0 q  i5. 生成所有可能的组合(回溯算法)7 z- y0 ~- f" o! X( S
    - g2 m% [3 r' [$ O- N) b$ ~9 R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒," r+ C9 G+ e: a: C. J3 ^, Y; V
    我推理机的核心算法应该是二叉树遍历的变种。! D+ s6 C+ A. ^1 t1 h; q
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; L( {  o8 T3 n6 l& f, G2 D
    Key Idea of Recursion
    2 c( m4 y; L! |/ l' S9 L  ?+ e; a5 g* M3 E# f
    A recursive function solves a problem by:; c9 o8 y- ^; q+ d  }5 m

    5 u9 {; V/ F) Z0 b4 m    Breaking the problem into smaller instances of the same problem.
    * I- S% O/ k# c: t8 F' ~0 {/ G6 G0 I. j) i8 t
        Solving the smallest instance directly (base case).
    9 n! o. C8 K- m8 q% t3 X+ w3 \( V3 [: f6 S( w
        Combining the results of smaller instances to solve the larger problem.
    + @7 S5 s3 }7 y; l2 Q+ j
    0 ^3 l; ]5 S) h$ VComponents of a Recursive Function
    + }9 E( G( R/ m* ~
    ) X! U* u; J$ t  a6 H  i% b4 c) E    Base Case:
    ; p: ^& N* t9 ?  _; h+ c9 K' N8 }9 C( K
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    " c  t; k/ C7 r$ ?6 D+ G) a  ?; Q; ]# ~8 Z& @. Q* E+ w
            It acts as the stopping condition to prevent infinite recursion.
    3 T+ N4 Y) n, o# n
    " c2 }; K- W* O0 ~9 o& R! ^6 e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ) T( t0 m; v# H3 ]2 }; R& t9 f2 B
      _0 T/ X# P6 J2 a2 M    Recursive Case:
    : g( Y) J4 u+ @: }
    ' t) T3 k% X6 \3 n        This is where the function calls itself with a smaller or simpler version of the problem., \6 j0 v9 \) ~/ w# M/ _
    " J$ C3 F* S/ n0 F4 G2 C
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    - x6 ~7 ^: d3 W0 C/ z' c+ k  v
    % @. k% z  s. l( R( D- d: YExample: Factorial Calculation2 R6 j6 e) M9 [2 F, b: S0 @

    ; S9 H6 S: J5 R( u, XThe 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:1 S* l: h- I9 I' d6 F- d+ ?
    4 p2 _# S& u% n/ {  s
        Base case: 0! = 1
    ) p$ x- q9 Z! t, ~
    : i2 Q( `& l8 w1 _! t    Recursive case: n! = n * (n-1)!
    % i6 A: R# @- w* K4 v
    3 B( d( p0 f% g3 X+ W6 JHere’s how it looks in code (Python):
    8 f) k9 m0 j( [" opython
    # B0 i; @& H, |. c( v+ L- z+ H
    ( Z( ~5 k0 q' C" I0 D+ T
    ; f& `! O! H7 z' O7 ?) p0 L' qdef factorial(n):7 e: n  i. ?8 j0 _, U3 q( g1 [+ O
        # Base case
    8 q8 n3 b+ G  k! ~6 G% H& m    if n == 0:6 u" l1 _& Y% g7 W# U% O
            return 1
      \4 f4 ~. U1 R' G    # Recursive case. z6 ^* g3 w5 V' T5 T5 M; m
        else:
    ' H2 T+ D2 x  S; t" }        return n * factorial(n - 1)
    ) m- _2 l. B! Q, t; i1 N6 p5 n) W2 B" }( U
    # Example usage
      B1 P7 h! Q5 Fprint(factorial(5))  # Output: 1201 Z; t. b! \  ^$ J- X; a) l4 {
    5 M6 ~$ h" B0 t+ Z/ ?
    How Recursion Works" {" J% H$ v9 u8 T" `; Z+ W7 ^- f

    # N  T9 V# {$ M    The function keeps calling itself with smaller inputs until it reaches the base case.
    . n. S( C  s9 B, p/ x1 Q  Q6 G. a: `. Q& p
        Once the base case is reached, the function starts returning values back up the call stack.
    9 X' W6 O6 E9 w/ D0 s2 {: w# i( O/ j1 N1 S5 d0 |6 X: U
        These returned values are combined to produce the final result.2 ?! Q% L1 |) q8 G3 y" p( }

    0 x5 R" e& a2 U/ t! v* h; N7 {; yFor factorial(5):
    , S" r3 _% K. A; a- d' w. A0 o/ v0 M- `) m3 b
    6 a3 G. C" g1 e4 c
    factorial(5) = 5 * factorial(4). X* ^: y1 r* s" A5 k' K; ^, i
    factorial(4) = 4 * factorial(3)
    ! P2 L& ~' U  J& s/ nfactorial(3) = 3 * factorial(2)5 B7 Y4 U7 o/ |" O0 W
    factorial(2) = 2 * factorial(1)1 l2 Y' S+ Z8 P4 N" X% T$ n7 v2 d
    factorial(1) = 1 * factorial(0)7 K) f, [1 x: h/ f
    factorial(0) = 1  # Base case5 |; B6 Z# ^5 h  C1 v
    - M% J8 Q9 i* [+ ^/ D
    Then, the results are combined:
    # n3 ~7 t/ J; i% D0 K  A' {* b5 k" Z, b" s6 t4 ?) Z
    * i6 X1 c! l# S9 X
    factorial(1) = 1 * 1 = 1
    7 d& o) u, e6 l" F! l: Yfactorial(2) = 2 * 1 = 20 R/ `* ^- A8 t: ^# T* D# O
    factorial(3) = 3 * 2 = 69 g, I7 u+ [% B/ J+ ]
    factorial(4) = 4 * 6 = 24
    / P6 X/ q. S' c4 k  ~$ hfactorial(5) = 5 * 24 = 120
    . H3 J9 }) v' y  g# q1 I* [7 I/ J4 F- O8 P. D7 o
    Advantages of Recursion
    - I( r# ?7 o7 q( f4 o
    ' Y4 I0 H( K0 q. k  _. U4 B4 W    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).
    3 u0 j, I* }+ q1 @) w
    ( d- g) w' y, ?, U& s/ ]    Readability: Recursive code can be more readable and concise compared to iterative solutions." V% \4 L  n$ |* ]# W
    / w) D- H3 D" D- b3 |) V
    Disadvantages of Recursion0 _) \/ g& m& E2 j- G- S/ ]6 z* P
    9 r+ _( d+ u5 W8 h1 r0 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.
    , F. Y) Z) M: O( _3 e
    * T+ Q0 w7 w, @( V0 x+ G0 O    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    5 c# a" F. @8 @; ]
    1 X( P4 B$ Q  Q  B: oWhen to Use Recursion
    ; S# Y! @: k1 f6 x$ B5 P1 U3 s; l7 x1 X' }& d2 f
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 o2 x  m" N3 C  l6 a
    , `! q' m8 u: T0 c2 X* `7 ]7 I% k
        Problems with a clear base case and recursive case.4 D9 r- q# A: g; u9 ], }. k
    3 n& n# k4 c# V7 {% j2 v+ n* p3 `
    Example: Fibonacci Sequence
    3 Y6 @. ^9 c0 _- k) Q# T0 c0 }8 t# B: L2 m5 T! b0 {8 o! L
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ z$ x0 U' J9 A" c& w/ f. r! C* G
    $ \8 ~9 [2 v9 ?+ }9 U% ^
        Base case: fib(0) = 0, fib(1) = 14 s7 k7 ~/ Z9 K2 \* U: e' w
    3 W5 b( [% I" p& K- A$ w
        Recursive case: fib(n) = fib(n-1) + fib(n-2): L2 m' }( e1 b) `7 Q

    : c# d5 t+ u4 W7 v  t1 bpython
    ! b1 A$ A: e0 X/ ^2 ?/ b  h7 U( I0 P$ J9 x& j

      Z# Q2 ^4 g; C" ?  z) odef fibonacci(n):
    8 @$ j) o9 ~, k5 T1 @! W! q    # Base cases5 B  q. T% W7 A' z7 N
        if n == 0:
    7 K, h7 C/ w5 B1 V' {        return 0& U/ {1 `+ K& X
        elif n == 1:
    5 y: C" F, K. s  H" A5 U& G        return 1
    ( u3 k) ~; ]2 {: q    # Recursive case2 C& B; u& ?2 {4 B' a& F
        else:
    ) w! s1 x* j) m        return fibonacci(n - 1) + fibonacci(n - 2)5 {+ b+ F2 W6 o8 {  {1 f8 h) F$ h
    1 I+ T1 o* O. q/ i% ~
    # Example usage
    . z. q' B: c! ?6 e9 V* Vprint(fibonacci(6))  # Output: 8
    - b0 [! i& ^/ k) I5 x; p) x( k9 A. L& r- K
    Tail Recursion
    ! Q) T. L( H* L
    3 M# _8 e; _- e/ r# \& OTail 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).
    / O) g" m( z% G' C0 p5 a% ]- G3 h8 U# Q0 }
    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-11-23 13:40 , Processed in 0.030901 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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