设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 8 |. z, n, P8 t: V" U
      I+ x' H4 N, i5 t9 b' w
    解释的不错
      e2 a- X# X  s; @4 d4 }
    * S9 m* k1 M2 t0 Y0 Q/ F3 j& ^递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ h8 J  o4 M) E  G  U5 |+ _8 W

    * ^1 n2 V) `; y" | 关键要素
    2 d- J5 p1 C/ i1 h) I6 S0 u- {2 J8 _' v1. **基线条件(Base Case)**( C6 w4 P8 [) i3 J; L/ I
       - 递归终止的条件,防止无限循环
    + G' B/ X" ~3 K5 k/ U+ X0 ~   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1) W8 w4 S9 a: i3 J% t
    / G- p# t4 e& |- C9 {) ]# a
    2. **递归条件(Recursive Case)**% J, s' Y7 G" }6 e5 c7 _
       - 将原问题分解为更小的子问题- I1 f! J3 |9 l$ y& w& G5 n
       - 例如:n! = n × (n-1)!
    & d1 B, ]0 s3 ?
    - C  f$ h: N' p 经典示例:计算阶乘
      z0 ]! j) i6 F0 Dpython3 [! L5 S$ U8 ^3 X- j
    def factorial(n):( \2 I  n) n! l& A. K- x; p
        if n == 0:        # 基线条件) Z. L- A' }8 N" W* R4 e! ?* U$ r
            return 11 \. S( g; U5 u$ X6 Z1 R
        else:             # 递归条件2 ?$ ~8 A; e4 B5 b
            return n * factorial(n-1)
    8 C* B3 y' P9 n/ H$ R  R执行过程(以计算 3! 为例):8 Q* q  S) Z) k. ]/ P" S1 p
    factorial(3)
    6 J2 S+ Z( Z2 ^! m! {3 * factorial(2)
    " c2 \6 W8 G3 n4 H2 l. F) d3 * (2 * factorial(1))
    9 N6 {4 x! l7 K3 * (2 * (1 * factorial(0)))" z3 |" \4 f" B7 r/ a
    3 * (2 * (1 * 1)) = 6+ }) H$ @/ n! e! u8 t
    5 I( E& m5 U" N* n
    递归思维要点
    - Z- X, c6 i- H1. **信任递归**:假设子问题已经解决,专注当前层逻辑; ^& A/ @* c) o5 {% @
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    & [7 c8 f8 c! l. H6 G$ w3. **递推过程**:不断向下分解问题(递)6 ^& r9 l: [0 k- @  q7 g
    4. **回溯过程**:组合子问题结果返回(归)- r6 {0 w/ e+ d: C. {  L+ d
    : f" f% [2 `4 E+ P4 V
    注意事项
      v( \& n0 p- n; \  ^2 \& f$ J必须要有终止条件
      O' Z: n! D9 d& l0 B递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    7 @9 g) m, ~- l; m6 j2 U) j. V某些问题用递归更直观(如树遍历),但效率可能不如迭代- Z' X8 U% }  a: M; q; `- `+ [# ~/ b
    尾递归优化可以提升效率(但Python不支持)9 x! W. q' C- J8 Y

    & p2 e: m; M$ D0 E1 i 递归 vs 迭代
    , A! h! j( E; W/ y|          | 递归                          | 迭代               |
    6 n; c" k0 A- p|----------|-----------------------------|------------------|
    6 Y: |# o  G$ ~' n7 v% || 实现方式    | 函数自调用                        | 循环结构            |
    7 A" ?1 W: t7 T5 ~9 h' O( D+ Y1 r# j' W4 w| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ( @: ]: G& w  ^3 X) q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    9 t4 Z, U+ A' |5 Y| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    3 Y; _5 A6 R' J" z. u- A
    ) I# h) V8 @5 [3 v: C; m0 \ 经典递归应用场景3 @9 V$ L- E' O' d5 U0 W7 E
    1. 文件系统遍历(目录树结构)5 @6 T# O) ?, I9 ]
    2. 快速排序/归并排序算法6 ]! e: I+ c7 G9 l+ m
    3. 汉诺塔问题6 C  n7 ^# L# C
    4. 二叉树遍历(前序/中序/后序). o$ V' L/ B9 c% }  S) [; ]2 P
    5. 生成所有可能的组合(回溯算法)9 G# V1 ?3 ~2 q& O$ P+ m! y( @5 l

    : t8 U; G1 t/ y试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 07:29
  • 签到天数: 3183 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 D! |  F$ J: H6 E2 C, `& P, |& H
    我推理机的核心算法应该是二叉树遍历的变种。
    0 H7 q* w7 e' [& l# G4 M另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ! ?/ M' [$ T) {. FKey Idea of Recursion
    6 X/ v/ D0 x" |1 }# s  h' ~5 S. q5 D5 U/ {$ }
    A recursive function solves a problem by:
    3 L  P* w. K( `- C! }( X# I6 J! G  V& r6 ~
        Breaking the problem into smaller instances of the same problem.
    ! \# b' i8 J+ g" b+ A$ E! u/ `$ D2 v4 l  R5 G. x
        Solving the smallest instance directly (base case).& I" a, h, e. A9 {

    $ X: K( @+ W! H3 \6 c  u1 q, g    Combining the results of smaller instances to solve the larger problem.
    * x* @0 g. ~& F
    . p4 f9 t# A, x1 VComponents of a Recursive Function! A) f0 q9 s$ L2 ?
    ! b/ S3 J* e2 t. P; k
        Base Case:+ z$ d( D/ {# B/ C1 @  _

    , d6 ^" D9 X% i! l" R1 q1 k        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ; g( f# p' Y  C" P+ K
    ' W5 R4 b/ Z9 W: \. A0 D        It acts as the stopping condition to prevent infinite recursion.3 f% j  f' G: c: }! [

    * b5 M/ u* c6 x. x2 [; ^9 S        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.2 k: a  P& |! P2 I% q, I

    - n( o$ R$ v8 q0 @& a    Recursive Case:
    4 }- y) E$ h! q: G% l
    3 B& m. f5 f- Y. m        This is where the function calls itself with a smaller or simpler version of the problem.) B+ f" \2 n7 V% z  H) S

    - u4 x+ [( P' T2 d3 W6 q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " e$ G! }# ?0 i4 f( m& @8 }
    6 `6 T0 {- ~: L8 Y9 @2 lExample: Factorial Calculation
    : F. G2 h% W% d; S. W2 w  d
    5 k0 U# ]* \& u8 f8 dThe 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:
    : P, h1 o# w4 d4 Y( y3 w2 [3 B2 e. K" D; m# Z
        Base case: 0! = 19 d/ y" _% R  V4 K% j. L4 v

    7 I4 C2 o5 M7 v    Recursive case: n! = n * (n-1)!
    7 e( @, m& z3 w# l; |0 @( p8 R9 i. W' K" Y4 }3 K9 f* [$ ^
    Here’s how it looks in code (Python):0 w5 U& r3 a! y7 `, a
    python
    - D0 S/ h( d2 ?& e* q
    , B. q% x  s( Y& E4 T1 F/ a! B$ t, k5 O3 F
    def factorial(n):* ]# t2 S( }4 w4 l8 y. @2 v; J' ~
        # Base case5 q# D" o7 |2 X; x# j( l. a
        if n == 0:
    % c) s. [, O  |0 q5 T% i        return 15 u2 j9 n5 G0 V% F4 e$ \/ @0 t
        # Recursive case  x& H/ y/ s- e* H0 [
        else:
    3 X( [7 t% G) B0 O8 p        return n * factorial(n - 1)7 p$ c, o( r3 r* A# s
    3 s) v+ L, ]* T% `: p0 k9 H
    # Example usage
    ' W1 S4 Q) X: V/ P- O1 P9 J% ?9 S+ lprint(factorial(5))  # Output: 120
    + M: K" N% b, p8 U8 D& I  [( Y! z, t( K* K& i
    How Recursion Works) ~, Z/ L; u' C- S* ]
    ! C) j1 M' `- [$ s1 s/ g- |3 _
        The function keeps calling itself with smaller inputs until it reaches the base case.
    * z+ z/ Q% L1 v$ Z8 ~
    / K, u& g" z+ D' x$ K! s8 {6 n    Once the base case is reached, the function starts returning values back up the call stack.
    7 z4 |* v. S; N# W, f6 U1 ?2 k. |# S/ J
        These returned values are combined to produce the final result.; N- D+ h: A5 n0 q! l

    ' |* Q9 @  q/ u0 WFor factorial(5):3 r/ Y7 l) D( k
    # f8 |6 \1 L. z; J% q/ a" x

    $ Z5 D- H+ N+ J0 e8 zfactorial(5) = 5 * factorial(4)
    4 @. ?. S+ `% x! Jfactorial(4) = 4 * factorial(3)8 ^8 y1 ~- E7 ]8 c
    factorial(3) = 3 * factorial(2)
    6 f0 A( d: E, k& tfactorial(2) = 2 * factorial(1)) U( e  l1 a* b5 s( Z, t/ j! S
    factorial(1) = 1 * factorial(0)
    & w# s( Q& J3 f8 A  ]7 Jfactorial(0) = 1  # Base case% E8 o4 w6 M- W
    5 ]1 ~; L# ]( c& ^; z2 w# ?
    Then, the results are combined:
    : a& {" m8 `) i  m8 k( N, K4 }- Y/ ~. f! [* M$ k( ]
    5 U7 j0 v9 f+ Z) l- O
    factorial(1) = 1 * 1 = 1
    $ c) R4 O1 K6 _2 I% ~factorial(2) = 2 * 1 = 2
    ' J2 s4 R9 g, Gfactorial(3) = 3 * 2 = 65 i/ u% h9 W- R. ~8 I6 {* V
    factorial(4) = 4 * 6 = 24* t9 P+ @+ @4 c7 d
    factorial(5) = 5 * 24 = 120
    5 d; }& o6 T0 g0 z$ \* H* h3 ?% o% V
    # ^9 p, z; @+ }. e5 SAdvantages of Recursion
    3 Z) F8 Q& L7 P2 E4 C
    5 u$ R: V7 T# ^6 \( 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).
      N5 C0 f4 I9 [$ e: @" b. ], Z
    3 d2 A9 `% w, P) `    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 @+ t/ w1 |9 ?3 d1 v" F& I  G; d( a, O2 d  @
    Disadvantages of Recursion2 [4 m2 G% \, _9 P. B
    " ]- Y3 @5 t. I% r3 x( [1 F
        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./ V+ K8 @! p* u) ]) G

    , N3 n. q; ~& A9 G, o) B    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    5 }) g# K- L  L% o2 x. ]
    - u) h+ m7 X* _: `' f4 x( ZWhen to Use Recursion
    7 \8 O0 S1 ?3 Y+ p4 B1 [$ t" d9 I
    : y+ W! G2 ~: D    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 M' F8 |2 C- ]4 d0 \0 M' K) R! i
    / W  z; R# g8 X) R% S7 r
        Problems with a clear base case and recursive case.' F5 B% c# u- O4 Q4 S) U- x5 l- R) K5 U
    2 C8 s# w5 B; w: Q) W5 {* P
    Example: Fibonacci Sequence* s2 |" V. J: c- S
    . O; p1 c! u/ o4 ]5 l6 J" _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    & ~3 p% U- }, d3 j) S) P
    & I. u# o( }4 a, y    Base case: fib(0) = 0, fib(1) = 1) Y" j* i% j7 Q9 B

    ( f% A0 Q+ h: e. l; K$ B9 Q5 c7 m  f    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # B% b% l" Q5 R1 i5 `3 C) v1 ?; w2 [+ l# J4 s7 Y3 x5 W) n; f
    python
    ' _' Z- t6 l- E( A% z4 m; z0 ]4 W7 ~
    " @0 T( A6 V' P9 R# G9 }) R
    4 z3 j$ S0 \# X! C8 D% ^def fibonacci(n):: f# U2 I' |- b4 ?! Y: @
        # Base cases
    ! Z5 [2 Q9 e- T' [7 Y    if n == 0:  Q( t, J1 p' Y+ D! G4 R0 V
            return 05 y9 ^' R  q; i2 y1 ]4 I2 a3 p
        elif n == 1:9 R  R& V+ F( a& @# e4 o2 U0 N
            return 1
    4 Q+ `7 z, j+ `/ {; ?6 U    # Recursive case/ j$ x! O, E5 g1 \0 s% O
        else:( r) {, w6 {- D
            return fibonacci(n - 1) + fibonacci(n - 2). Z$ Z) u6 B6 m7 i; e$ n- P' r

    - A+ l$ c- g# O- B# Example usage% n2 W1 ]+ i& Y$ L! a5 p1 o+ ]4 A
    print(fibonacci(6))  # Output: 80 O, D: z. {. B$ `. M0 p1 D
    + g/ v9 }0 P4 D/ `: ~* o
    Tail Recursion  b3 V: o& u' ?7 W) N: P

    5 ]% S$ e. [, v7 I+ ]& e6 d3 L( [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).: J3 f' V% l1 C7 E) s$ \2 i' T0 F! A

      n6 `) y6 A( B2 d$ v7 k: gIn 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-2-25 06:31 , Processed in 0.071767 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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