设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 / t0 E$ o+ Z5 A- W

    ; z/ X1 ~% B/ R& |解释的不错
    * G3 n( F" m4 Y4 l/ E2 L* O" M- k  `0 k! [1 P$ g, x2 X
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    + d3 k' J5 {+ D' L+ Q3 E2 n' ]8 \( {- ^5 r
    关键要素  ^1 g0 x! m0 [& _, r1 o
    1. **基线条件(Base Case)**
    ' w2 G9 L4 G( m6 n   - 递归终止的条件,防止无限循环
    7 K# L. M" G, T8 b, ]  W   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1+ z+ F- k1 j4 r
      s. L3 w; d2 E' S5 h
    2. **递归条件(Recursive Case)**4 ~+ n8 Z8 l: L
       - 将原问题分解为更小的子问题8 N+ n' B7 s" |  K( S
       - 例如:n! = n × (n-1)!
    & ?9 L) Y1 ?: r9 I9 Z9 }) z
    ( M% d9 b7 O. T- R5 L 经典示例:计算阶乘
    5 c5 O; z# K; j& v' gpython
    " s% ]' ]' N- d+ W/ @4 K/ X6 Sdef factorial(n):
    ( H( V8 @- j. J! S# G4 k' S$ o    if n == 0:        # 基线条件  h0 Z4 N# V' D/ _; o
            return 1' a. I; N0 J/ \% `: T- _: B
        else:             # 递归条件2 y: s# ^5 r5 m
            return n * factorial(n-1)
    # G% F1 b% \5 g  s  h执行过程(以计算 3! 为例):
    ; e2 j9 y- B# K; }factorial(3)
    6 z2 Y4 J' u) z8 k3 * factorial(2)& T0 Y# i$ K  j8 U' k
    3 * (2 * factorial(1))
    # _3 F+ j0 {: T" ?( n3 * (2 * (1 * factorial(0)))4 Z6 T8 i. d& A0 v
    3 * (2 * (1 * 1)) = 6
    0 z2 P; @9 j0 F  \  r4 @. m
    ) l  d3 {( L( K 递归思维要点) D: a# u' G) E5 P7 t
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . Z% Q; ?0 @- M2. **栈结构**:每次调用都会创建新的栈帧(内存空间)6 T/ J4 W7 e# B' L  x- }; ?
    3. **递推过程**:不断向下分解问题(递)
    1 j9 U- _- O& {; O7 c% U4. **回溯过程**:组合子问题结果返回(归)& {0 z, s. h* M  D! W

    3 Q1 G& m# a" B" u* |) v+ | 注意事项
    ' D+ U4 z" L* q必须要有终止条件4 |( |* k+ `5 q4 s& A
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    0 m. ^% T! Y# u某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 t2 o7 h$ s/ Y( @$ r6 j% T尾递归优化可以提升效率(但Python不支持)
    $ i& A8 P) N) @" g! `' ^
    $ }0 \/ f6 l/ y 递归 vs 迭代
    8 M' [; |  f8 Z( b|          | 递归                          | 迭代               |
    / g6 j7 K: |2 C|----------|-----------------------------|------------------|3 a# t) R- U& L- ]; T  \, M
    | 实现方式    | 函数自调用                        | 循环结构            |
    : U6 ^3 Y% U8 \( || 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & z0 ^( E4 \& V" A| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |# {& t% S1 a; p  H
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |$ Y1 I; A' K: e) g( w8 K7 h

    , F2 U9 K2 j1 N  K 经典递归应用场景1 w: n1 d! z% P: p+ z8 C# S
    1. 文件系统遍历(目录树结构)& e5 S. e1 L& |
    2. 快速排序/归并排序算法
    7 Z/ Q' P+ S% T3 j# j/ ?7 `3. 汉诺塔问题
    ! H/ E' X- b. Y3 W1 C0 Y/ L4. 二叉树遍历(前序/中序/后序)
    5 q! w8 q' a0 b4 d7 A! ~3 I, Y/ e4 v5 z5. 生成所有可能的组合(回溯算法)! P) Q% n1 ?4 |" o0 \2 z  l

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

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 07:12
  • 签到天数: 3185 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 V, N3 Z! D, _. b- E
    我推理机的核心算法应该是二叉树遍历的变种。, O0 S1 m- t. ~# m2 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:
    ) M0 O- D* s4 g4 {) Z# fKey Idea of Recursion2 G, \' k- ?8 B6 z. w& Q8 \

    : M+ {  ^: O& r# l- aA recursive function solves a problem by:$ Y: Q4 @2 V, [
    , |* n) H: Q; W' i6 A
        Breaking the problem into smaller instances of the same problem.6 l) O3 ?1 {) y" n4 ]6 c

    . y/ |3 J/ ^7 F1 P" `    Solving the smallest instance directly (base case).
    3 l' I9 d9 d0 p4 n" j3 Y) p7 m  }1 A* B/ S" z% @3 g
        Combining the results of smaller instances to solve the larger problem.
    - o! I# ]7 }, l( g, w& c5 c: j& o/ o7 D) H9 l8 M
    Components of a Recursive Function
    ) P* |( p$ G( ]9 O5 b
    , q$ O2 v8 E; h- d7 C( l    Base Case:
    $ c. C' @( g4 d& S* y
    + y& H, s1 S$ w5 Y! z$ I( W( j        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    6 g9 ?, G# O+ j8 A$ X5 C' h3 `; R: P: l' n' I( \5 V  U2 ], e+ J
            It acts as the stopping condition to prevent infinite recursion.
    1 P$ v7 ]8 ?5 \  R' P1 O/ O9 S' R  T3 u2 D
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' @* `% }# T) a- l

    1 }1 \" o/ K8 r0 |. f    Recursive Case:
    4 `. o. Y2 I* ]& Y! M. g, t: A7 j" S1 a$ v" U9 M; J7 J! G
            This is where the function calls itself with a smaller or simpler version of the problem.1 m1 e1 P& B2 b
    $ l' D9 e9 o0 @* |- S: m
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).) L7 x6 v0 \4 C$ n' n6 `( i* }! s
    - J, j' ?8 z* d) }. |
    Example: Factorial Calculation
      n" z+ E' ]$ C' V: T& s* ~& r
    ( r  }* q  K4 S# |0 sThe 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:
    ; L+ [6 J( {2 l% Q# ~
    ) V# b, _. g  _    Base case: 0! = 19 N3 n0 o; r/ [+ ]- A: |5 J
    / }1 K) v  S1 w" J8 x" e  q9 Y
        Recursive case: n! = n * (n-1)!9 B1 A! _7 O# N0 i" z9 W
    * i. O# m8 E, z* y# ^
    Here’s how it looks in code (Python):
    . I1 g/ y4 [: H) q% ?( `! y" `! [' ppython
    " ^9 B  n" n' R9 x- x" y2 ]0 t' R! V+ P5 |$ W7 t

    * g9 P1 ]' P- w) B% vdef factorial(n):( F" b3 H& a( \
        # Base case
    ) b7 K! m7 _2 r/ ?- y" L( R    if n == 0:3 N* z! n" {' c# t
            return 13 \! O! ?$ {0 `
        # Recursive case
    * N% \3 S/ w$ k$ W    else:
    3 c# c  W9 k% Z: r& {        return n * factorial(n - 1)
    4 O3 q( O+ U4 D9 l0 j% D' E3 ]( S
    # Example usage  r3 _/ E+ y1 h$ {; _
    print(factorial(5))  # Output: 120+ n* [' ^1 @* g) j

    / c% v3 U! S3 ~  S/ p) b8 K% D0 N! FHow Recursion Works
    9 d5 P/ X$ q) e3 x2 i! }% u: D7 x$ j$ I. C, P6 L& x
        The function keeps calling itself with smaller inputs until it reaches the base case.
    4 |( {. l* |, i. U' b
    6 p# \% R! Z/ e" Z7 {: N/ f: f/ y3 F    Once the base case is reached, the function starts returning values back up the call stack.. n6 @2 g5 i* N; p. @# G* A3 s

    , N% @; H1 u% S- m0 ^    These returned values are combined to produce the final result.. [: v( e, p7 F

    4 _0 e7 F+ {; iFor factorial(5):
    ! n( l9 e1 _! a: c
    + g9 L: W$ `# x8 S4 w
      q# B5 \* i7 A% j1 f3 ^factorial(5) = 5 * factorial(4)1 ]/ v1 [- ]* c* Q% A* R8 ^
    factorial(4) = 4 * factorial(3)
    8 \4 I3 F' J! u9 R4 G7 C4 W6 [9 Efactorial(3) = 3 * factorial(2); i* I7 B# D4 E7 N5 U: B# ]
    factorial(2) = 2 * factorial(1)2 n/ L% b& P, b# U% c
    factorial(1) = 1 * factorial(0)5 o5 Q6 ]# J' t$ K6 l! P
    factorial(0) = 1  # Base case- k5 ~3 J: O7 X% |1 M0 u
    8 a4 ~4 I5 [4 X4 l: m' S
    Then, the results are combined:* ?& R: z: R* p5 G0 [

    1 b( R. f* z$ n" U  m  B; ]( s- [1 M$ R5 l( e, A( @* m/ H
    factorial(1) = 1 * 1 = 1* O: v5 x6 p' g2 x* y
    factorial(2) = 2 * 1 = 2
    4 ^+ k3 i9 k: rfactorial(3) = 3 * 2 = 6
    / h8 o$ i" x5 F0 Efactorial(4) = 4 * 6 = 24
    6 P+ E+ ~- b3 Vfactorial(5) = 5 * 24 = 1207 I- m% @) ]7 {; j- c) r4 T

    ) f8 @# w+ S9 IAdvantages of Recursion  p' X2 H; G' p" [& {% S
    + L( e8 G1 ~$ c6 l5 b# O
        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).
    ! O0 l" N: h. W8 x6 U9 ~9 P. S2 W( \0 d; b: \; h9 f
        Readability: Recursive code can be more readable and concise compared to iterative solutions.- z+ T1 O3 x: j9 f

    1 y: }$ b8 q9 P, |, {1 b9 T! qDisadvantages of Recursion
    3 }1 `- u  _* B; ], \" T% w! m' y# C9 b. y/ b/ e& N4 }
        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.
    0 D( V/ e1 h. z: C, ]  g2 O+ G: Y- S+ L  J
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 @5 ]& v8 Z) C0 W' C
    , ?6 y( f, K; O( }, j
    When to Use Recursion% }4 @! Z2 c, N0 z+ r/ O" `
    8 H3 P; a; R- `7 z$ j) e
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    $ ]/ G$ q7 B1 q2 Q, O9 }
    , L4 y) u3 r/ n, T) e5 G    Problems with a clear base case and recursive case.5 g$ E" ~$ X7 R# h
    5 D) b* d+ E& m( V6 x
    Example: Fibonacci Sequence
    " N& b/ g$ ~# p
    : v$ d4 A6 [! e1 E3 `The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) _0 D4 ?6 D" Z9 a+ x; I/ D
    4 y* F% Y) T+ g% `- \6 e4 P1 ]* D3 j% z
        Base case: fib(0) = 0, fib(1) = 1
    + j: U4 X4 x' G# A/ w' P* _: i$ }3 T' V3 I3 R- n3 v2 E3 N2 q: a
        Recursive case: fib(n) = fib(n-1) + fib(n-2)$ e$ X/ @4 J7 D* p9 P% }2 `
    1 u. i: L+ `/ x; c& n0 f, P
    python
    8 N  `5 Z& W' J- N+ Q
    7 T' ]/ n9 d4 X- S& D
    / s, ?: Z8 t: f9 }5 s. ^def fibonacci(n):
    ; J" Q6 I1 O( B3 u3 Y    # Base cases0 H) U! _6 y5 @, M
        if n == 0:
    2 I( y% `7 A0 y$ t  h        return 0
      p8 S& E" J( q% W# Y' t, w! Z    elif n == 1:. I3 R) u  d0 x2 |0 r6 F
            return 1+ e: Z3 `/ q" q( A9 S7 J- F1 z
        # Recursive case* x- y. ?& ?8 f; d; ]7 e) ~
        else:0 b$ K6 Q' f( a0 l' q7 Q# G( Y
            return fibonacci(n - 1) + fibonacci(n - 2)# M# \* A4 \8 X6 C! k" X0 U
    2 W, G7 B1 b, z' O3 ?6 [" N
    # Example usage
    5 V. l( X) d5 e( ]: X4 ^print(fibonacci(6))  # Output: 8" v1 O  b/ f+ J9 U( }6 r' O
    ; o3 z7 X" Z, |
    Tail Recursion
    # g3 Z. O/ l' B0 c/ |& R! r
    ; F" B( ^" D7 M6 }7 xTail 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)." X3 J& R5 K" G, p. \
    ) ]* w3 c; q' D
    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, 2026-2-27 01:47 , Processed in 0.054990 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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