设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 U6 g! s# ~, Q9 V# c- y
    7 l, D) A7 I. T) y- p解释的不错
    0 O* {+ B  o9 l" W
    , s$ G9 P8 K0 w  e6 \递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& w2 Z6 L- D- j, A' l+ l2 U
    ; z5 K. w; B( ]
    关键要素
    2 k; H# j* G9 @0 f; n! @2 E1. **基线条件(Base Case)**
    5 s5 G% @5 n8 X% g8 p   - 递归终止的条件,防止无限循环8 s- K# u1 Q# G, T8 Z) E: @/ C4 G
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1" v) I8 {; ^1 l. X" S3 U/ I; W6 s4 e$ D
    % {/ y2 \) ~& t1 f4 b8 ]* f2 ]6 D
    2. **递归条件(Recursive Case)**
    - ~* J$ T5 |- k1 I# |! G   - 将原问题分解为更小的子问题
    / ]8 X- ^+ {  D4 {  X5 b   - 例如:n! = n × (n-1)!
      u1 d/ }0 r- w5 P
    1 ~1 x) `  j$ c$ k 经典示例:计算阶乘$ w" `+ m# u; @: w
    python2 R! T; k% z) e% w" ?$ c' b: ?
    def factorial(n):' g" Y1 p; ]( n8 @" N6 A
        if n == 0:        # 基线条件
    6 K) T! w' R/ N  m! V+ F7 `, G) a        return 1
    9 n. E7 q* G4 o; r& A# z% z% ^    else:             # 递归条件- k6 F0 s- X+ S. |( e
            return n * factorial(n-1)
    % w- Q2 w! t, ~  W) ~/ _执行过程(以计算 3! 为例):
    4 |; P" h9 R" Hfactorial(3)$ k- v8 K) U/ j8 |* A
    3 * factorial(2)
      U# ?* _+ F  O) x3 * (2 * factorial(1))
    + J' c" F1 p* q2 A" u3 * (2 * (1 * factorial(0)))$ M" y0 `. l: `# b6 B8 h
    3 * (2 * (1 * 1)) = 6
    $ _: S+ ]. N( O4 t# k' m
    6 h0 Z; @4 I' e3 z# G 递归思维要点
    4 f0 a0 B, T+ I/ F1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    9 U* a! S3 g+ G9 e, W2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ [# u: P6 s/ T6 c( J) j3. **递推过程**:不断向下分解问题(递), d5 i% b1 U' E) l, @0 `) S
    4. **回溯过程**:组合子问题结果返回(归), f9 V( z7 [9 F' l& ~1 I. J4 |
    5 [( s& B$ B2 K8 l3 u* {4 E0 G! H( ~
    注意事项
    ' E* S" |/ d2 C; m; Q3 V( w, ^必须要有终止条件
    4 q# x. E# N% }. m0 ]递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! T! C" {- _# D某些问题用递归更直观(如树遍历),但效率可能不如迭代
    - [+ e6 G8 z- A. t) k尾递归优化可以提升效率(但Python不支持)
    9 ^4 l( d. t/ I
    8 K1 T! i4 r( L: [ 递归 vs 迭代
    5 B4 Z$ L3 d3 {/ D* W! T|          | 递归                          | 迭代               |
    4 p7 L% W& b- i: i' `: c|----------|-----------------------------|------------------|. p# O/ G% s- R# s2 D2 o# x
    | 实现方式    | 函数自调用                        | 循环结构            |
    " Y0 O5 ~0 w' r| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & S0 C9 P0 T. v. |2 B| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ' B  z. C1 v- t' G$ q- k| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
      Y) n2 Z& Q2 z/ x9 O
    7 y* k# ^- R" m4 `% D6 N( l' \ 经典递归应用场景$ |$ ^* v$ f" N
    1. 文件系统遍历(目录树结构)$ O9 j: c. p0 k* n
    2. 快速排序/归并排序算法4 z+ B7 J1 t" d8 p( [) @
    3. 汉诺塔问题
      D( _+ K* @3 N( q* O# c2 i4. 二叉树遍历(前序/中序/后序)9 v3 Y6 n+ s, p2 y# R9 _2 C# J& G2 o
    5. 生成所有可能的组合(回溯算法)- B% p$ r6 w3 `

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

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    16 小时前
  • 签到天数: 3212 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,4 {- T( U: d6 `% x4 ~! S7 s' g
    我推理机的核心算法应该是二叉树遍历的变种。# w; e, P; [, w
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:7 F/ C8 h6 t5 f( Z
    Key Idea of Recursion
    $ o( M+ p9 s% S7 M% \/ `8 u3 P0 r3 g% s4 F2 z% K
    A recursive function solves a problem by:" Z. ~; C: i0 h

    4 H/ Q% y1 B8 e* Z+ s. {    Breaking the problem into smaller instances of the same problem.: l. q5 U$ y0 }5 x' e* `7 R, r

    0 M, R' w! T: ]. q% `$ ~    Solving the smallest instance directly (base case).
    + |! P7 B7 n4 n; G
    5 b% J. x! _2 V7 y& [    Combining the results of smaller instances to solve the larger problem.. U7 M! k8 u- M; @2 N
    * z7 _7 j/ C' V8 S' l7 Z0 I! D  k
    Components of a Recursive Function; m( H  t# T6 {

    $ K8 K0 h! I0 u7 p6 U    Base Case:
    1 d8 ^' p  {0 Q; E& T4 W, I6 R9 b* Y' l& o, `$ u$ s  n  i0 _5 q0 `1 T: R, B7 j
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.9 V- f5 m  Y0 p; A- O1 U, Q

    1 Y9 x5 E& Y$ e$ H        It acts as the stopping condition to prevent infinite recursion.( Q3 O: N( k5 ]. Z  s

    4 ?# }6 j  U# R6 u0 D        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - ]* G- S; L8 l1 L. o
    ) B4 Z9 E. ?) r    Recursive Case:
    4 P* [% r' `3 S: f& ~
    3 x: {. T0 |/ v+ z        This is where the function calls itself with a smaller or simpler version of the problem.2 E6 _( y; m/ X) L8 t

    ( v- a1 l! @$ V$ f9 V  z# n# g        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).0 A3 I) s' p$ d' \3 v; K

    ' t$ e, w5 B3 X  r% s# vExample: Factorial Calculation
    : |7 e; Y  [7 H6 v6 N0 v# }4 O! g
    % a" w) a9 R2 MThe 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:
    , C- p. T, S1 b
    / q$ A# x+ S% l; M$ b$ ?    Base case: 0! = 1& u. i: y+ P5 _' _* I7 p8 A* L0 T
    6 n+ K! \! ^4 H7 S- l( F7 D3 @
        Recursive case: n! = n * (n-1)!% _) p$ u% m5 [( @# k3 @
    3 z$ [4 ?6 a' a# z7 m
    Here’s how it looks in code (Python):
    2 b: h  Q" ?0 k, Q$ }0 lpython
    ' v/ L. g/ _( y1 p1 K
    ) p: G# L+ I; s8 B. e- e" v5 y; o/ i- s- C/ O
    def factorial(n):% `% ]( h3 R- l' L0 A
        # Base case
    ; f/ S; Z+ U+ E    if n == 0:
    5 s1 Z3 S8 C( }: n/ }! |9 _        return 17 G0 Y  ^6 r! W4 o
        # Recursive case
    ; k% p2 `) f  }0 }/ j    else:
    $ d6 O! i8 f& \! G: h2 r6 U" _        return n * factorial(n - 1)8 g6 [7 l  ~% u4 Z/ Y

    0 S& b1 g" |+ n# Example usage8 L) R: \: r$ U+ Z% {7 T; f
    print(factorial(5))  # Output: 120
    + F9 x4 V: s2 l; E7 S0 ?* @* z" F) t4 V8 q
    How Recursion Works* o' Z4 J5 u; S! N4 g

    3 ]7 S8 e+ Y. b  t6 ^; E6 e$ d9 y    The function keeps calling itself with smaller inputs until it reaches the base case.. s/ w) d( {5 `9 z& ]

    3 t7 f; j: B+ ?    Once the base case is reached, the function starts returning values back up the call stack.
    6 y; L4 D5 [( Z% l0 J% g0 Q. x5 F% u3 g; O/ Y
        These returned values are combined to produce the final result.+ y* ^- c9 A. O$ Q
    7 r2 J3 C2 R; E1 G! I
    For factorial(5):3 ]* ?. |( `* M, q( a" J
      k: s% Q- J! D0 L8 d

    9 ]9 a; Y$ S/ |2 q# v( W5 J7 ~7 afactorial(5) = 5 * factorial(4)
    : T5 T  _# ^! a" g6 jfactorial(4) = 4 * factorial(3)- |5 }; |% M* X: S
    factorial(3) = 3 * factorial(2)
    ) C( V* `/ a$ A$ E  z. h, G8 A) ]factorial(2) = 2 * factorial(1)
    ' t: P( p% E" H* T$ Q# G9 ]factorial(1) = 1 * factorial(0)
    ! {8 w1 Y2 H! m( S7 nfactorial(0) = 1  # Base case3 W6 T8 U* r- ^3 g
    9 s! m& }2 J9 m% x8 V+ R0 K; _1 E/ l
    Then, the results are combined:' P/ F6 ~3 J8 Q/ a- D7 P

    * Q" u  z% y2 {' {. J* ^( i
    & o5 G' T6 M! d! n4 [factorial(1) = 1 * 1 = 1  C! C: H, Q8 E. x* \  F
    factorial(2) = 2 * 1 = 2
    # E+ S) s' U: k  U7 @' rfactorial(3) = 3 * 2 = 6
    ; Z, a, D* Q: ^* sfactorial(4) = 4 * 6 = 24
    9 B" t* ?7 [4 G0 \* Ufactorial(5) = 5 * 24 = 120
    . [' n- ^3 ^3 n3 ~" q1 Z7 `  v! n+ C/ ]- [7 K; {
    Advantages of Recursion
    / N2 f' R- @; @: ]0 L* B
    , q! h5 v3 b+ |1 t: X, D    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).
    # K' Y  z7 B* r1 H1 O9 V* c6 y5 e. o+ k& f& L
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    : R) z  Y% R2 q; n& j
    8 ]) S4 T6 [5 rDisadvantages of Recursion. B/ i! u* j/ Q. X% N
    2 c) a. O7 X! m3 k/ |2 D2 X
        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.8 D* ~( F, ~% p; y: y( K. ]
    9 @- a5 _* X3 `! P( _
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' R& @- n  r# C( D8 k$ r# T2 V- }' I' Q; [
    When to Use Recursion
    - j# m& T$ J9 o& I
    ( K; q8 T3 `' \1 d3 Q3 B" j    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 s1 n& w3 b+ L! e+ t' a* |! G( T( B1 y$ \  H; S4 a% @
        Problems with a clear base case and recursive case.7 ~5 k9 V- ]# [2 T0 d. n3 _. U

    . ?5 }8 R! K6 [/ _5 M5 pExample: Fibonacci Sequence, g/ Z( m4 Z& c
    5 _; j& ~* r* o
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:8 Q' P. P* J8 a& J% j' n: U

    4 P' u7 E* d6 n    Base case: fib(0) = 0, fib(1) = 13 D4 y% ]$ C0 D, ~9 H8 w( ?4 K3 K& b
    2 D4 o: F* E4 G6 |: B2 {
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    $ q* _. x8 \8 U  u2 {( \* ]4 s/ I" o0 X; p
    python
    + q3 j/ I  ]6 i" K3 S+ V- m$ F9 d5 K. W2 [; _7 G! J+ o: G
    ; T) k# F+ M) Q
    def fibonacci(n):
    ( s2 c4 x7 S- ?+ J+ K& R. T    # Base cases
      f5 P$ i1 c7 l- b# l( ?    if n == 0:' q2 z0 Q' A7 V3 H" {
            return 0% |" U" o9 S2 v; o3 J9 s0 F% v1 d& n
        elif n == 1:
    $ K# s6 _! \2 O% r5 W, ?2 i+ A        return 1
    2 P. W; r2 e) r. L    # Recursive case
    4 H. R, {; S4 z    else:
    / ^: `# a. t) M  F  c7 U* ?+ S        return fibonacci(n - 1) + fibonacci(n - 2)9 n( L$ \. Z" `1 J" U

    2 W; E( h; N/ y" M8 r* I# Example usage: A0 Q% \( a1 e$ a5 J; l
    print(fibonacci(6))  # Output: 82 ~! c+ z) D8 T) n
    ! E: B2 d6 u" \$ d; ~
    Tail Recursion( J: W$ y" i9 L

    * Y3 M" V, X% w3 r! }! JTail 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).
      @) G: K1 w# S' V
    , y8 D2 `) R: QIn 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-4 23:14 , Processed in 0.063643 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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