设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    . ]1 J. C7 b. a- c, i
    / H" j  A$ ]! M. c: s4 M解释的不错
    : _7 \" Q& o! `1 W" s$ Q5 E$ R+ Z7 |
    # l4 V; V( W3 O. Y! x递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。* P8 U* g6 R& Y! y: [& A
    , {7 b' z% \) O" m' w, y! U
    关键要素
    ; O: R1 v6 C; s1. **基线条件(Base Case)**( s; |( }; O) F% h% {
       - 递归终止的条件,防止无限循环
    + c& S: x! j* W   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    1 c  a% F) S# [; E0 b" Q
    , B% F$ H8 c8 u5 z2. **递归条件(Recursive Case)**
    + F* \: [- K2 O* y' M3 j& Q   - 将原问题分解为更小的子问题& S3 D& c+ @% z& S
       - 例如:n! = n × (n-1)!; ~5 ]2 ~3 B$ Y$ {( u

    ; a, S2 R, E: r* W4 E: Z 经典示例:计算阶乘2 W9 @5 s/ x* J4 D
    python/ t, O$ S8 G' V. y8 ^6 d$ R$ G
    def factorial(n):, s2 f' q+ v& b; I% S
        if n == 0:        # 基线条件/ P9 X0 P& k7 k" k
            return 1
    8 V5 u& T0 F+ X9 _/ @) s    else:             # 递归条件+ {+ o! ]4 A  P0 ?7 M* K
            return n * factorial(n-1)
    # O% H8 B0 D2 T6 H) q  M( L# o/ l执行过程(以计算 3! 为例):
      w% {+ T1 v8 T; b6 [, gfactorial(3)6 F0 N" e% g* x  E9 b
    3 * factorial(2)# x: |8 n* e* J6 T' K4 {& P# e4 h
    3 * (2 * factorial(1))
    2 s2 c& r) }. D4 Q# e8 H6 M! @: [3 * (2 * (1 * factorial(0)))
    - |% c2 Z: i& [' y6 B3 * (2 * (1 * 1)) = 6
    * c' y1 n4 C; M# {" W5 n  t
    ' I2 b* D; d5 n/ O7 B' z& P0 u5 ? 递归思维要点
      }: |/ e7 q' K' K: z' I1 N1. **信任递归**:假设子问题已经解决,专注当前层逻辑; C& v0 p; k, E- k9 t
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 Y2 ]" d3 |& Y
    3. **递推过程**:不断向下分解问题(递)
    ! U3 m8 \# L. \) L4. **回溯过程**:组合子问题结果返回(归): B0 N" u2 n9 [5 \2 x+ _
    $ v; U2 i: c3 T( O" T. I
    注意事项8 A8 O% P3 z, p1 r7 j, U
    必须要有终止条件
    7 _+ X/ m) I! @! _! I递归深度过大可能导致栈溢出(Python默认递归深度约1000层)7 a& H8 v& Z0 W8 f6 L4 k
    某些问题用递归更直观(如树遍历),但效率可能不如迭代! T( J" h: ^: u) l
    尾递归优化可以提升效率(但Python不支持)
    2 C0 K- a. A% x9 t" p7 M) [
    ) Z0 w# Y* |- w& h% Z 递归 vs 迭代5 Q! P) w% a6 o# Q8 \
    |          | 递归                          | 迭代               |; R: u6 [7 f. W! `  z
    |----------|-----------------------------|------------------|
    5 D+ R" k& W+ B2 w$ k* ]& j| 实现方式    | 函数自调用                        | 循环结构            |
    0 q1 X9 n3 p4 A! G+ z2 `8 {| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |; v% ]7 B3 G. b3 d5 J+ Z
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |: q( c& \1 @$ |& l
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |& L+ I# u" k$ s  i0 u7 {
    3 N0 z' V6 y  ?* _- v; O
    经典递归应用场景8 t7 ~2 o% W& M, r% H0 t
    1. 文件系统遍历(目录树结构)+ b8 V! g2 h% S' ?' d1 C3 A' H
    2. 快速排序/归并排序算法
    ' a. V; X! i+ V0 Q( J3. 汉诺塔问题: l! K  u5 K2 e: \1 h5 m) p! a3 F! T
    4. 二叉树遍历(前序/中序/后序)
    * c# t$ F2 M& L5. 生成所有可能的组合(回溯算法)) z& [4 x+ L7 h- T+ j+ b
    0 O) K, U; {! b) ?$ b. \# d
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    11 小时前
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,: ?& ]1 G( l6 X$ ?
    我推理机的核心算法应该是二叉树遍历的变种。# }8 b# `2 j2 ~' A
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:) X; K' @) H- C2 \: q
    Key Idea of Recursion, U4 h2 g  a: j
    3 V( Y( c$ r$ N  F
    A recursive function solves a problem by:# ~6 P! g& n' W; e
    . J2 _( y9 |' e: l4 b* ]+ w1 ?  J
        Breaking the problem into smaller instances of the same problem.
    / h- F6 U) G( X. o& w2 I
    " u+ X9 x3 l  g, ]' d: B4 z    Solving the smallest instance directly (base case).
    $ B, {' E3 r. ~9 q, @( {) @! z6 z( S* l
        Combining the results of smaller instances to solve the larger problem.6 u( f4 Q6 p9 i, _" v
    ! k1 G9 }5 {, R/ \
    Components of a Recursive Function$ t) B0 m# _8 H  I- ?' I
    % n! A, ~7 u+ O9 z
        Base Case:
      e$ o6 P# u/ |5 J/ y# d8 I9 r; ~. c! N* ]9 ?7 W5 K9 ?2 t
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & v9 I+ @. B) z! K* L! `+ j# L! r5 L* l' b: u6 D/ G
            It acts as the stopping condition to prevent infinite recursion.* E" D" r/ ]* u  v4 D1 J7 R
    ; g3 S( t! e( N5 {# ^
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& j' S: R2 p% q

    ' z1 e$ N8 W  a    Recursive Case:3 s) y8 h7 R! j

    ) g' L" d, b9 M  {        This is where the function calls itself with a smaller or simpler version of the problem.. D) u3 s! F/ B# d1 @2 u
    7 b9 S6 E/ [9 O, j* w
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ) N7 w8 }. U$ W" ]" O( I- M8 B6 k4 C1 I) }" ]
    Example: Factorial Calculation  ~6 d: q1 z0 i

    9 S- w; q( [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:
    $ h9 ?3 M' B' Y+ ?
    6 L- X2 H# l8 t9 G# P! N7 t8 a    Base case: 0! = 1
    8 t3 a8 K  `( I' R+ E8 ~. N2 t$ j- _" H' B4 z4 I8 s
        Recursive case: n! = n * (n-1)!! j! ]. A* D! r) [, w
    " }4 i1 o* v7 ?0 U% }( X/ U0 V
    Here’s how it looks in code (Python):
    7 g. q$ P5 P' {4 z8 |* l4 h0 ~, Mpython3 _7 B5 @5 U3 U# l0 r5 `

    2 ]( [  x% ]8 R  e: K2 ?  z
    / l! b" e" H5 l! m) ydef factorial(n):
    ( [1 }- c" V/ `0 t" z2 n    # Base case9 f6 z! g/ U$ e. j* I
        if n == 0:/ a8 U1 g) [* F5 k- A
            return 1& X+ T; E% C/ a4 o
        # Recursive case
    $ ~0 ~# x- U, X& w    else:: J2 h! B: m& I% r& {( c# }2 V
            return n * factorial(n - 1)
    4 w2 ^4 b7 v* F. o) A. ^9 H4 q/ R! d
    ! @9 \- B& N( Y3 s, |) x/ W# Example usage
    % F* p- `6 t" ^print(factorial(5))  # Output: 120
    0 V$ U/ `+ `" H! @! `- Q0 N
    ( ^* S# A1 w6 Z& F, v; u# b6 k1 [$ eHow Recursion Works
    & j. `8 \, u% |7 {( t6 U
    ' ~: Q1 k2 Y' X    The function keeps calling itself with smaller inputs until it reaches the base case.
    ( k1 S4 k3 W: [* |. p
    9 Q8 ~# i2 _! q/ w4 {3 a    Once the base case is reached, the function starts returning values back up the call stack.: {8 I8 f/ K( e( X: D+ \
    ( V  I2 v% _! h9 j4 \: d
        These returned values are combined to produce the final result.- W- ~7 i" _' Y& z

    % \! ^* o; f- s: M: d% PFor factorial(5):7 W8 ~: i% [; x1 M

    + m8 y' H% U8 U- B* \6 W% R  s3 n) O2 d* q5 z# Y; h  b2 I
    factorial(5) = 5 * factorial(4)3 C4 S1 k+ Z# J$ |
    factorial(4) = 4 * factorial(3)
    - d- ]$ D1 i# Nfactorial(3) = 3 * factorial(2)6 m2 u; ~+ n. _1 a% T/ j. F" B* X
    factorial(2) = 2 * factorial(1)# {. w0 \/ s1 I, {. W' n' N0 J
    factorial(1) = 1 * factorial(0)
    ! V* T1 F: M6 z7 [6 V8 v1 y3 O0 G1 Vfactorial(0) = 1  # Base case) |, |5 @# D( T! t+ [8 R

    . F" x0 T; O3 o( JThen, the results are combined:8 l9 w+ y9 H- Z  _
    ) ]: X  p; u+ v0 v+ n6 o0 A
    2 X" l9 m. T# z  g9 q
    factorial(1) = 1 * 1 = 1
    : n; D( v* t- i: X" ~factorial(2) = 2 * 1 = 2
    ' j3 j$ N( a5 i" Mfactorial(3) = 3 * 2 = 6. i; R- e( n) c
    factorial(4) = 4 * 6 = 24: X0 Q- s2 u& I! O8 v1 z" I( ]
    factorial(5) = 5 * 24 = 120
    ) p2 _1 c9 Z6 P  r3 l! o+ c
    8 Z: I! b) F- W5 z* w$ fAdvantages of Recursion0 a/ K( z4 a* r

    ' o  z6 P/ q) X) o, a+ U    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).
    * Q$ s. ]7 L1 N3 ?4 W1 }- ?2 ~  B9 C6 q/ ]% l; F% C3 f2 F
        Readability: Recursive code can be more readable and concise compared to iterative solutions.. s! t$ _  A/ q/ X
    0 h4 R+ m0 S! ?& M2 B
    Disadvantages of Recursion
    . H) P0 t0 A9 @, d$ Z- q! |/ x" z/ S- x/ K9 U0 [( @+ e+ s5 k5 C
        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.
    5 _1 `( K+ ~9 G* L; ]1 @: V) K/ O1 p) Z6 P6 i
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    1 z0 L3 u+ w& |4 l$ k  a' `
    8 V4 @8 ~6 E" B7 c8 o9 f6 A4 [When to Use Recursion% _, X" b0 K, P& j, H& \/ \3 V0 ^8 ?
    # s2 L1 A* G3 I
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).* r+ z5 H8 T1 V+ \8 p1 S
    * x, c  W; A, }# r7 y( K
        Problems with a clear base case and recursive case.) j, p- o- x0 p0 i

    ( o: I2 j8 w( @8 I1 YExample: Fibonacci Sequence/ x; s1 T0 m0 z4 \1 M

    2 F: E, X; L. NThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& g7 F! H( N$ C* S, E- S, Z
    $ M6 ^8 Y4 o. B$ _+ w) ?
        Base case: fib(0) = 0, fib(1) = 15 @0 E1 ]) M" f8 f1 ]3 E, g5 a% Z3 r

    ( w. n. `' U7 n5 v3 M1 C& G    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 c0 S2 u% D4 G% u% g$ O6 b3 b) u9 k) a, N
    python
    & a- `, Y# j/ ~' R- ]3 |2 K8 F! s' y% N* ?

    , k4 V# p/ [0 h7 O: }* ndef fibonacci(n):
    ; B* ?$ ~5 Z5 Q/ c8 x    # Base cases  `; L+ I; z0 d2 `" [6 x8 m4 Q
        if n == 0:: Y4 V5 K* r) ]/ C7 @
            return 0
    % t) t. {  O8 w8 C% Q/ T3 _) E& F    elif n == 1:
    2 j  ^  D1 {0 q  r! ?! b# [        return 1
    9 F7 \% G4 f( U; x    # Recursive case3 d% D- H& b$ `9 D: F$ f
        else:
    1 [. F( E1 Y4 j        return fibonacci(n - 1) + fibonacci(n - 2)
    ( K4 N1 @" p" f  s
    + m0 B9 Z) l5 @# Example usage& W- j7 @" p" J# K) t# P
    print(fibonacci(6))  # Output: 8) w5 r* r* m) J) u: b2 l

    . w% V% m  w/ O2 z1 nTail Recursion
    8 T( c5 f& Y. G! ?# s3 S
    $ \5 F) Z! \: ?* R' e( z" vTail 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).
    & h9 [+ W, M9 X: i
    * ~5 p0 r3 x9 N, tIn 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-4-9 19:45 , Processed in 0.059928 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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