设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! c4 p9 {8 t6 _3 _3 I6 t
    + K" g5 {" ^  K: C- V解释的不错
    4 p5 `0 U1 ^# X3 u
    - z1 R) S' d: J8 p/ d- P8 t递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ R$ i# K: ]; K9 b
      d8 f8 T) {- U
    关键要素* K& V1 u& B3 L8 F: Y! Q5 s
    1. **基线条件(Base Case)**/ k3 Q+ B* i. e; H8 @
       - 递归终止的条件,防止无限循环
    $ l$ V+ T" v% _9 T4 p7 M, f   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    6 f/ @; r9 E" s0 l4 n
    & V6 q- J( @9 D1 K: L9 g2. **递归条件(Recursive Case)**2 c* H  _4 q5 k. x2 {7 K+ W) ]
       - 将原问题分解为更小的子问题
    5 X7 H5 d3 ]) E0 p, j2 v   - 例如:n! = n × (n-1)!6 N3 q, ~" x% l5 w1 f
    ) L8 b" B, [' W+ h/ [( o3 y
    经典示例:计算阶乘7 L) |: I6 u; k- i) v2 N/ g% x) ^" k
    python  _) R" E+ i& g! U3 C7 [. b; x. @0 K
    def factorial(n):
    & r( i6 L- G0 ?8 [    if n == 0:        # 基线条件
    . ^: r! x! A6 Y, e" R        return 1
    7 z  z* n& m: u+ E0 g' q* k/ q+ p' b    else:             # 递归条件4 |4 L7 }) W' Z6 u- h- I
            return n * factorial(n-1)
    . F! u/ {5 B# o执行过程(以计算 3! 为例):
    8 F" z% G0 l2 x1 i; e6 nfactorial(3)
    & g3 P6 ~& }2 q0 n3 * factorial(2)
    1 F+ w$ o& U" T0 G& [* Z3 * (2 * factorial(1))& A: ], Q7 q) ~$ Z- d9 E9 h
    3 * (2 * (1 * factorial(0)))
    ' f1 R- b5 V" W0 O3 Y3 * (2 * (1 * 1)) = 6
    ( Q( x' \, O2 s# n( R8 g0 u
    7 l$ s. l9 q$ O! S. ?) x( B$ a' w 递归思维要点
    . e8 Y) O! Y! q: `) T4 n1. **信任递归**:假设子问题已经解决,专注当前层逻辑" ~; y$ L+ I2 Y% S
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    & ^2 U$ X5 d/ V$ f3 O3. **递推过程**:不断向下分解问题(递)1 I9 W* q/ N0 r0 o0 R
    4. **回溯过程**:组合子问题结果返回(归)
    $ ~/ g/ x) ~* L& r  u6 e! x$ R5 _! K/ T
    注意事项
    & i+ i! b% x; Y2 O4 T, e7 Y! w$ o( z必须要有终止条件
    1 e" o7 m3 P' e8 G3 U0 o$ X递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    . X( b# O2 V9 ], x+ [: T某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ! O9 W6 a3 O# [6 i4 ?3 d尾递归优化可以提升效率(但Python不支持)9 L9 M3 |0 a& I$ {) v& h$ B

    - ^) V  F% h  U! I! m; s7 |4 K- U 递归 vs 迭代8 x  X$ ?! x( c; F
    |          | 递归                          | 迭代               |
    6 @! v6 a& ^) r1 ]2 t. N; h" _|----------|-----------------------------|------------------|
    & `& I' v( K- u; ?! B, S0 R+ b| 实现方式    | 函数自调用                        | 循环结构            |
    / y7 G) f/ n& g$ n4 J% h| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    % Z- \& O1 y/ I( ?9 Y7 e3 E+ @| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    " I2 e- C$ E( D+ ~2 W+ G" O% u| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    3 l  k. Q# z* S  S4 r! A  X7 ]  l+ q& o0 L, r
    经典递归应用场景
      k( ^. j) s/ V, v; Q1. 文件系统遍历(目录树结构): n( H4 \# y# K& c( z- q
    2. 快速排序/归并排序算法
      B8 ^* K3 a2 C" w3. 汉诺塔问题! u! i/ `- A$ o5 N
    4. 二叉树遍历(前序/中序/后序)
    $ k' n: _, m8 w: H9 a3 Q& `& ?( {5. 生成所有可能的组合(回溯算法)
    . x) x" L! ~# M# b( h9 I
    : i) ?9 C0 y5 \& C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    15 小时前
  • 签到天数: 3144 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    1 e) I( y3 @0 q$ |: `* Z% P我推理机的核心算法应该是二叉树遍历的变种。
    5 ^4 W7 U7 R; x0 s5 i/ o$ T另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:8 N) m- l1 H. q5 Z
    Key Idea of Recursion
    ; _  A+ J" `8 M& G8 w2 B0 O
    $ N. N, X7 B5 L6 t0 T  A3 o2 z" oA recursive function solves a problem by:0 h( ?; S8 |* I2 s9 P$ h$ @4 |
    6 P" ?/ K1 E2 Y  d. M3 @6 ?9 E
        Breaking the problem into smaller instances of the same problem.
    , ~2 \. a( Q. [# g: A% o- }$ x1 {
    & y- z& ?# {% ~& }$ Q) t    Solving the smallest instance directly (base case).
    2 R8 K% O, G/ D; b( i# ?3 ?' \1 z7 l" H
        Combining the results of smaller instances to solve the larger problem.
    7 _; j2 ^$ L# m
    0 l8 K( L5 w$ Z+ ^; TComponents of a Recursive Function0 K$ D6 m$ Z& J4 X! [5 {2 T5 w
    * q! U1 W$ O, K
        Base Case:  D+ ^& R8 p) }$ z" |2 Z
    . j& A$ a+ M, r; n' Z2 T* P
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    , E5 j4 X: B, k, ?3 |5 R+ H# L( n4 L, I, A+ }% g. P! t
            It acts as the stopping condition to prevent infinite recursion.1 o! z' |3 }! i* K8 @1 G; L: D

    1 g/ E# @( Q# e. ]        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 y' N# L2 b' }* d: e, a  O/ _+ Q- Q( l, c1 U, ~
        Recursive Case:
    7 e( l* L1 ~7 L' }& M! i4 R9 G: u) f( w, }, z7 {6 e
            This is where the function calls itself with a smaller or simpler version of the problem.# K9 W) w# x) B$ U1 ?' v4 {

    - C$ }* F. d: X3 |) R. A        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 ~3 ]: m1 ~* u+ }! o. s2 E$ \
    8 K- q9 A; |# p! S* N# b0 C  Z4 gExample: Factorial Calculation
    4 S2 q5 D  o: t/ v! \& _3 ?2 Q3 @0 u* C
    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:3 Y. k4 _# c2 C: J6 B$ D& z
    * ]' N3 l9 k8 l' B/ O$ ]& Y
        Base case: 0! = 1
    4 J7 m9 A  L8 x5 a; v; c" a/ e
    , Q+ E# A% O/ p" b    Recursive case: n! = n * (n-1)!
    # y! p- _3 k8 b6 g* \4 u" |$ `* ]/ |( S: w- k' z' q3 U- V
    Here’s how it looks in code (Python):* ~% u# t5 M  V( ?1 t
    python
    2 H$ E2 k% j. R9 U5 x) r8 O% U" ^6 D2 L9 ~3 H; g6 G1 r  [4 C
    & @0 ]/ L4 Q  u: Z
    def factorial(n):4 ?/ s7 l2 L' ]1 I$ @
        # Base case3 \( s) A9 D' I5 i9 c
        if n == 0:. n% Z8 X  z& f* X* w! j& D6 H
            return 1
    : Z0 H! U# m& R    # Recursive case& L6 _9 O; U1 |! D$ D' X
        else:1 Y' ^3 F6 O9 D# `9 I* X
            return n * factorial(n - 1)
    9 o6 k. Q1 n9 b& W
    ) C2 S. p  s" T1 J# Example usage
    ) v" X6 d# @, ]print(factorial(5))  # Output: 120
    ! ]( j$ |; U% D) R+ F. P
    + }+ |* I2 k$ v' w% [, E, o$ jHow Recursion Works
    4 p0 Z* E0 Z& }6 k4 R+ f$ U
    : g* F. Z: X8 E1 x) i: u& |0 {3 l    The function keeps calling itself with smaller inputs until it reaches the base case.7 @7 q) @- Q0 X! ]

    5 G6 e% ^. U8 V# j. W: u    Once the base case is reached, the function starts returning values back up the call stack.- j3 u! R# T9 p( e

    . O3 v9 z0 b/ O* k& v- f* C0 B    These returned values are combined to produce the final result.
    $ E5 r8 P; Q: \  M! h) P6 }
    2 u4 \5 z! O- Z; QFor factorial(5):4 {8 ^3 _) Z* F
    1 `6 i8 G4 S" C$ q0 Q2 A- W& L

    % \8 D" b: O  M% }' ~% m& _0 jfactorial(5) = 5 * factorial(4)
    , f3 F" r9 A* i6 t& T  Y7 S! k2 sfactorial(4) = 4 * factorial(3)# f) k) P. v9 K! d/ N
    factorial(3) = 3 * factorial(2)! o  j7 G: j  P! M  t: ^
    factorial(2) = 2 * factorial(1)
    ( S, [, ^2 m- _2 ?  f5 {factorial(1) = 1 * factorial(0)& I3 f0 l$ C4 k- Q
    factorial(0) = 1  # Base case5 @% ]& ^& G# v5 f

    # O, O3 |% w1 i' f9 yThen, the results are combined:( \5 K" k, u$ C7 d3 @! N
    . O: x+ ?% x# Y1 {. h5 \" V" C

    " W' G  Z4 q2 s$ H1 v" Ofactorial(1) = 1 * 1 = 16 [/ N2 ^5 E" ^
    factorial(2) = 2 * 1 = 27 }; B9 I( Q* B
    factorial(3) = 3 * 2 = 63 j; g  ]! g9 K  |, ?8 [
    factorial(4) = 4 * 6 = 247 L/ S7 M+ M$ ], B
    factorial(5) = 5 * 24 = 120
    # X  Q& ?* S1 E  C$ G5 p7 E7 `1 h2 P+ P9 p5 V
    Advantages of Recursion) i# k  [  d; `" [: u; d

      d- C  x+ b* p9 P& 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).
    " E) i$ X# _" B% p
    4 B  r! U! X  H" N, E  I    Readability: Recursive code can be more readable and concise compared to iterative solutions.+ B4 X5 O* m) j/ m
    , @8 I  v# D; d: a
    Disadvantages of Recursion
    6 C1 D( ]- {; ?0 b7 ~7 `4 q* y9 Y4 Z7 O# ^+ w
        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.
    * \' u9 h' t: M( F4 K' ^$ r% j$ s: d/ z: c6 D
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 c  H) ], `% `% Y# P8 e# u
    ) F; i6 W9 l2 H/ M5 w
    When to Use Recursion0 j: g6 [) b4 Y1 b8 K' D/ N( f: m
    ! [$ }0 y( j  X  ~& j
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).0 x+ h5 y% D- ^/ K% f5 Y  }
    0 q- W  w5 O: A
        Problems with a clear base case and recursive case.
    # t* [$ Z3 u! V1 ^7 Y1 w6 e; F2 N# E1 f  w$ i
    Example: Fibonacci Sequence3 m; Y( L- o5 Z/ d1 y# f4 v
    " Z0 J- m( W" u7 Y3 u0 o" ^2 V( f
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ h  X) x* j# b% V6 V1 I

    & ]( |, n+ ~9 Y) U( h    Base case: fib(0) = 0, fib(1) = 1% O1 k  B! F* W) H' O0 \) h( W
    , j9 z! q- b0 ?4 [, g
        Recursive case: fib(n) = fib(n-1) + fib(n-2)1 S7 U$ l2 A: f- [8 Z' Q( |

    % \3 P, z2 S3 O1 N! U) jpython' h3 m0 j9 e% S$ i! d
    ! }4 _/ d& S0 n4 {; x" i1 I( U9 M3 |

    9 H1 a. I7 A( u, t1 Z7 Q( }; idef fibonacci(n):
    + i+ w" ]) y6 J; C/ r; Z    # Base cases6 g+ q- z$ `) R$ s7 ]) r- ?- E( U
        if n == 0:
    / ^/ b6 N( L; p1 }: N! z6 [. P# y        return 0
    + ^0 S$ f; J9 j' f$ v* ~# f3 i8 }    elif n == 1:/ n1 i, B+ K8 w$ K% x% s
            return 1
    * _6 y- g7 m+ D! [  |* m    # Recursive case
    7 f* Q7 M1 f% B9 t& ~  U0 {    else:" Y! ?" Z' K+ i  D" b
            return fibonacci(n - 1) + fibonacci(n - 2)
    * S' h1 N1 v7 ~6 F1 L4 t7 I: n& @% t' P) u- y0 _; v: H
    # Example usage. y1 X2 N8 p7 m. C- r/ F# Y" _* Q
    print(fibonacci(6))  # Output: 8
    , \# f, M4 \8 f" m$ Z" B- u; P( u2 Y4 J; }% ~! ]8 @; f; r9 ^& I
    Tail Recursion
    # l6 T# Z9 e, V7 G2 r9 Y/ R( H1 W" _# A- c! G+ b$ ?4 j5 Q
    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).! a1 t2 k1 x5 f: L+ V2 b" d

    ' U1 t0 \: r5 Q/ W3 Q3 D% aIn 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-1-12 22:49 , Processed in 0.035517 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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