设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    8 F. g" q6 q/ h" R
    + ^: A, p0 ^/ }& m! A解释的不错
    : V0 Z( f" p3 `4 D
      m+ z6 x; ]: r8 _+ `  V( V递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! V' N  i% r" p4 Z" u0 [( F6 A& {

    ( k* k5 B; r& o( f 关键要素+ `# w  D' P; o! Z1 j0 N
    1. **基线条件(Base Case)**
    * \* Q7 Z; D% g& S   - 递归终止的条件,防止无限循环
    5 T! E% Y6 K1 b2 U: Q; M" D$ m   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ) n8 R) t5 B6 Q! Z# D/ U" e
    ( j, a" e7 E3 e% a+ b8 a& F8 Q( o2. **递归条件(Recursive Case)*** n" V5 s* |$ O2 Z! {& q: N- v
       - 将原问题分解为更小的子问题+ Q% h7 X& p  x  T
       - 例如:n! = n × (n-1)!& Q$ D' A7 t" R. a
    / z$ i3 Q5 w8 p- W
    经典示例:计算阶乘/ m) h+ z/ `+ D6 v+ w4 z2 k/ S
    python
    % m+ Q: ^! Q7 p) e% k- u0 Tdef factorial(n):
    2 g1 s$ V! c: j6 E7 \/ j    if n == 0:        # 基线条件
    0 [$ ?. C* n1 z% o1 K( l        return 1: _- U4 d* F1 g
        else:             # 递归条件. F" I( ~7 t7 z0 B2 `3 G2 w" e+ d$ k
            return n * factorial(n-1)
    / l3 x! c& H- @7 X! b% w执行过程(以计算 3! 为例):
    ; ]8 P% `  W1 b6 \factorial(3)
    7 H: H. P: E7 ?3 M6 P% {3 d3 * factorial(2)
    & W1 _* s. }( d/ k2 D. d* `3 * (2 * factorial(1))9 K2 d, F, O. ~8 M: y3 [: O3 W
    3 * (2 * (1 * factorial(0)))
    0 Q$ K9 M& A( Z+ k2 |9 z3 k3 * (2 * (1 * 1)) = 6! r8 ]7 L0 v# ]% u$ E0 _
    * X9 C9 m( P. i. y
    递归思维要点
    : D. X2 V: g5 n) t( [7 L1. **信任递归**:假设子问题已经解决,专注当前层逻辑* f" G5 I& G* R7 f6 X
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ; r  T  Y4 V% E1 Z7 m: Z3 R3. **递推过程**:不断向下分解问题(递); I9 Y( N0 V' G+ ]: r8 B3 y
    4. **回溯过程**:组合子问题结果返回(归)& }' [* r) x; {( _* Y
      D- y8 D- ]" b3 O. g
    注意事项2 w) Y% u6 v. ^0 d) ^
    必须要有终止条件
    3 e" ^$ Z/ g( o) L. A递归深度过大可能导致栈溢出(Python默认递归深度约1000层)% R0 z' k: c; z, ]  P" T0 _
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    2 S+ e! f& ?  A: y* x尾递归优化可以提升效率(但Python不支持)" X3 Z7 D1 C8 m1 i9 o

    ! y% s$ ^# `0 J& d 递归 vs 迭代
    1 R* k: Z( m  \  b4 G4 R: ~6 N- }2 ]|          | 递归                          | 迭代               |
    8 [4 g3 E3 d. q  c  d0 ||----------|-----------------------------|------------------|2 @& ~  [5 `! d) i9 M
    | 实现方式    | 函数自调用                        | 循环结构            |
    " i& J- y2 a- f. Y$ n| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ( ?* E5 c4 Y* j0 Q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |: z1 Q/ ~+ n4 Q
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    5 Q- z( Q9 N! n' ?* I0 ?, n; `: N$ t1 ^& N! s
    经典递归应用场景6 @  p5 T6 V: S2 Y/ Y  X7 i
    1. 文件系统遍历(目录树结构)' C9 u" l, c0 l8 p( a
    2. 快速排序/归并排序算法
    / u+ G; J/ j' ~( e$ o+ `! J0 j4 a' y3. 汉诺塔问题! N. ]# b( o6 G5 G( a7 o
    4. 二叉树遍历(前序/中序/后序), @) K0 P4 N8 L% Q
    5. 生成所有可能的组合(回溯算法)
    1 g! U* T: Z+ e4 _$ N
    ; F. d( Q+ X+ w- k0 F' ?试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 08:35
  • 签到天数: 3194 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  E' e9 w$ T- R
    我推理机的核心算法应该是二叉树遍历的变种。8 X& I2 l' ], x; t7 o" z
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    1 G3 v( G) ?$ g" w9 m) i1 kKey Idea of Recursion
    # H. O+ F2 Z7 l- j( m
    : S  J% g8 B5 Z. |4 n; H1 d% fA recursive function solves a problem by:" v: ]5 b+ {; {* y1 W" u
    ' |: u/ e+ F) u  w9 j) t  F, z
        Breaking the problem into smaller instances of the same problem.) Q5 G* Z* c% n. m$ z0 _5 I  P
    ; Z* `6 k. Q' n
        Solving the smallest instance directly (base case).
    + u7 N$ I* w$ M. c) E, J. P: _8 j7 P; d/ S8 j% V7 r
        Combining the results of smaller instances to solve the larger problem.
    / R$ c9 \+ r* Y5 s4 @8 \5 U
    - h' ^3 @. u, FComponents of a Recursive Function
    ( `# G8 b4 h" }' U- ?/ v$ e8 v+ G' M0 C
        Base Case:! u% x" Y! M: Z$ {7 q' ~( E
    . P7 ?6 \3 {& }3 m. S
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    1 _) E6 D$ `! Y  Y; |: ]( p( H4 Q% L  s3 V5 ?3 m4 c
            It acts as the stopping condition to prevent infinite recursion.
    ; J, d( u) X, F* R0 i* f
    7 c  j5 ]2 d  A" W( k" f& ]        Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ Y" i& F* s. ]" K! x$ s( _
    - L3 O, l6 u# O
        Recursive Case:
    * H2 A6 v7 E2 B$ `5 T
    ; M' e8 K$ k& T. @2 @0 N( _        This is where the function calls itself with a smaller or simpler version of the problem.
    & G4 h. H$ s( m! n1 ^& ^" |6 x" |: ^' o
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." d$ ?- e$ C; f+ n, G0 D4 _

    6 c/ f7 I' `* |Example: Factorial Calculation" ]& k. P& Z7 a" w
    ( y  l; ~+ C9 X* u8 ^9 l$ }
    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:
    # B2 ]! f' S& D1 u+ {# m: \  K  y2 g5 u& e: A2 w
        Base case: 0! = 1! g8 i& @4 h- F/ `) s
    ' J/ T6 A( P% |. \+ G( S. a
        Recursive case: n! = n * (n-1)!
    4 \: N9 F' h" L3 X4 h
    3 W  {: u/ m$ z6 oHere’s how it looks in code (Python):/ @+ |3 C( M5 I5 w
    python
      ]( ?( c% i  g$ A2 o- r- {% X7 ?! m" i' k
    . y9 f4 }2 A# y7 T+ P
    def factorial(n):( N/ N! N, _2 e' T  }# b
        # Base case
    1 {: g7 |. S2 F- I# u0 C    if n == 0:% d' n% h/ d* Y' ]8 \" e
            return 1
    & B! b8 }- h8 A' J/ x    # Recursive case6 h+ v) ]3 T8 n3 p: C2 D4 }1 ~. I
        else:
    / U1 n  M7 `1 X/ {% v        return n * factorial(n - 1)  z) S9 k7 I8 V8 S* J6 v

    / I7 g, V* j3 W3 P# Example usage
    ( P# f0 G" s1 a$ Q2 D7 S: _6 _print(factorial(5))  # Output: 120, j+ S& o2 `1 x: D0 m% o% L% ~
    5 J- x; ~7 d$ v$ l
    How Recursion Works
    ( Q  h" ~& _, O: j' q& ?3 ?" r% ?
    / |, o) h( g% y' P  m    The function keeps calling itself with smaller inputs until it reaches the base case.
    0 R" \$ ?% }7 p0 C+ z: R) k. j/ d
        Once the base case is reached, the function starts returning values back up the call stack.
    3 o4 ]0 d' d9 q4 h' \* f+ ]" L1 G8 M) }6 M! E
        These returned values are combined to produce the final result.
    $ u6 R! `# C; v' t
    ( W3 p8 P: b+ m6 XFor factorial(5):
      m2 [' {. a7 U
    / \# o* b) o' C' x! d# n9 `2 Y& Y- k) ]6 B2 P9 ^
    factorial(5) = 5 * factorial(4)- s; k- g& G- w& h  g! N$ R! ~
    factorial(4) = 4 * factorial(3)5 ?6 N3 X1 [% f8 P8 Y) G
    factorial(3) = 3 * factorial(2)
      B" e( U! |4 T1 l1 Z: rfactorial(2) = 2 * factorial(1)( h$ U+ W2 Y0 O3 p
    factorial(1) = 1 * factorial(0)
    , c, D% w) X2 g* f, v( U8 Efactorial(0) = 1  # Base case
    ) X% D- q/ x/ O7 T- M. N/ V7 o+ g  n$ z& ?5 b
    Then, the results are combined:: Y$ N$ @7 `% J# ?- C% Z
    ! m6 k2 L0 m* D0 ~! j% C4 Q
      }/ p& Z: C9 z8 o/ ]1 J
    factorial(1) = 1 * 1 = 1& U/ c0 r% `. V; ~/ W: \
    factorial(2) = 2 * 1 = 2
    1 P' u/ v& U' A1 O3 T) wfactorial(3) = 3 * 2 = 6
    # G" P1 s. _$ W/ K8 Wfactorial(4) = 4 * 6 = 24, E0 ?4 r8 X  ]+ T% K2 g  r, a+ O
    factorial(5) = 5 * 24 = 120
    " ^/ L! [6 P) W5 R# z- ]( }. R
    + Q& [2 F* S6 k- {Advantages of Recursion" Y# l8 G+ K# a& M( M) G
    9 e$ e9 O4 {8 p" g0 ?
        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).! y0 s6 y7 Y( X# Q/ R% r* }  E# ^# @1 e+ z
    * B! G; f" Y( N3 j5 o7 K6 _5 W
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " R$ W, R/ |3 @2 e+ \7 \1 A; M+ R9 J4 o4 ]2 O) a" ]; U3 \
    Disadvantages of Recursion
    6 c- t" ^/ \' c  [2 n/ o: t
    6 w. H/ ?1 R# a/ i* R    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 D2 q6 {8 E: g3 t9 J( E
    ) Y- q7 E' w4 c    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    1 W% `$ Y: z7 M' \2 Y, r8 J) @% L- Z$ Z. H7 ^
    When to Use Recursion- R( r* t* ]8 C
    : U+ H4 F* X- z. P
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ( @8 a% E+ Q# {- g0 I; o% d& b, C4 J2 L2 B, K8 q2 D% D
        Problems with a clear base case and recursive case.
    , u! s  \3 ~& z9 L
    * l0 D! Q& G: _. R6 uExample: Fibonacci Sequence) R8 c8 q! D. I7 e% _+ F( `4 D0 @

      w0 N' E! N' _# d$ ]The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ m( ?# n5 e% X% g2 S# U- k2 H, z: `' q1 E; ^* m6 [
        Base case: fib(0) = 0, fib(1) = 1
    , v$ |, W, Z) C6 U; A9 i& ?% \7 o$ z9 I
        Recursive case: fib(n) = fib(n-1) + fib(n-2)$ G- s$ R( C' t% P/ R# M) p
    ; {9 k" q; O$ D9 C/ I% I
    python2 k% X/ G7 U. h! I/ X: A. ~* j+ N

    ; F: D! H3 p7 K8 ?
    / U) q" }7 h; i2 b: xdef fibonacci(n):+ X# [& M: M6 ~0 I% P
        # Base cases4 ~5 n0 D6 v& s$ V
        if n == 0:
    " R8 q1 ~# p! Q) l( O! Q8 s+ B        return 0" [* J6 W7 C/ ?7 w9 F9 {' Z& G# h
        elif n == 1:1 o9 G8 Q& j8 [+ Y$ a
            return 1+ @# t8 B3 q: S. [3 ]
        # Recursive case
    . M, T* n) `  c    else:
    / G+ m4 R) v( [) x' ^- X: u: ]5 n        return fibonacci(n - 1) + fibonacci(n - 2)
    , d1 }% F/ s! B/ K
    + L! o6 J$ X9 W# Example usage
    ( T: l- v8 N& Y; ~- Zprint(fibonacci(6))  # Output: 8
    + S- G# j- l' p0 ^$ N8 U3 d) J8 F
    & G6 t4 ^* l4 A. h5 XTail Recursion
    4 |% h6 X3 ?5 `; P
    : g$ ?$ r4 V) P6 N9 ?% `3 @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)./ q4 d2 U2 C& \3 i
    3 F1 N* l2 a, `  c- j
    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-3-8 02:30 , Processed in 0.059758 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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