设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 0 k: G4 S1 x7 G. u
    1 ]' \3 g& ]6 U* t  {# [
    解释的不错
    ; L1 {) c2 W9 Q$ f, B1 j
    ( A& n$ Q+ ?. z% ^$ ~: C递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。6 M  a* T8 X( u) e: ?; X: C8 c
    " Z$ E9 I, l  K( n$ n( L
    关键要素/ K! K  C8 @1 ]$ Y8 _" e
    1. **基线条件(Base Case)**
    ; T1 Y' |  G' f- C8 `5 O1 ^' T/ A   - 递归终止的条件,防止无限循环/ ~7 p( N* y5 x: s/ v4 L) u' J
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ! \) c1 \0 _. D; |7 j/ N3 H0 g9 [5 b) W5 q
    2. **递归条件(Recursive Case)**" ?) |% ^/ ~$ R1 V( O
       - 将原问题分解为更小的子问题5 \+ m. k5 b9 j5 c: n
       - 例如:n! = n × (n-1)!: @" x4 J; C5 f. F7 r
    7 M1 ^# b3 q; {/ c5 ?; ^! `
    经典示例:计算阶乘
    * I# k: ?" f; c3 }' mpython# m( H; z' g5 f8 o" S* b' b) N. S
    def factorial(n):* B, P6 ^5 t2 b% y: J
        if n == 0:        # 基线条件$ G9 z1 v% u& H* f1 \9 a
            return 1
    , x; \  y% @- X3 c    else:             # 递归条件  ^/ u" v- q- [& |9 \# ]4 R0 {- ^
            return n * factorial(n-1)% ~4 X0 ~5 p7 t# R
    执行过程(以计算 3! 为例):
    8 ]5 X8 t& J" J/ [factorial(3)
    + i" R2 v  _0 e* B3 * factorial(2)
    7 R% U5 X9 M, }$ Z/ W6 O3 * (2 * factorial(1))! b; t5 {8 d7 z0 _; D& X9 H
    3 * (2 * (1 * factorial(0))). _# B; E  m( e' L
    3 * (2 * (1 * 1)) = 6
    9 w8 }1 S! P) {& N* Q" s7 j6 q$ L
    递归思维要点
    2 w% e6 r0 q* b# g5 j1. **信任递归**:假设子问题已经解决,专注当前层逻辑% L! d& ~  u7 K0 b0 g) A2 {
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ r0 @# |8 \2 b. [2 V2 E& {: O3. **递推过程**:不断向下分解问题(递)
    5 q3 {7 H6 Z+ t# y* G3 X& r" F4. **回溯过程**:组合子问题结果返回(归)5 R) ~. f- c( |2 D
    * x$ d* ?* ?  G- H7 ~' ]7 L
    注意事项# X9 G+ V% d5 d
    必须要有终止条件8 W3 }/ c+ h" z" K- `" E+ s. Z
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 Q4 p8 x! l" x& I某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , b0 h/ B  ]6 S" A7 y尾递归优化可以提升效率(但Python不支持)  y  b) V$ _5 C, P

    ' ^, l. }& U; o. { 递归 vs 迭代  H8 V/ ^6 X+ Z: f& L" ], ^
    |          | 递归                          | 迭代               |
    9 ~) m# ^) ]( c% i! H. ^|----------|-----------------------------|------------------|& N2 C+ B- X: V) A; m
    | 实现方式    | 函数自调用                        | 循环结构            |  e, E, t0 G5 r2 _! P2 m9 U1 `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! q0 f: ~9 T/ R4 O2 d7 R| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ H. E/ i, |; y0 x4 V8 M5 p
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 Q9 G; b/ @" m! ?  X
      z3 p( a, p: U: t  R4 W
    经典递归应用场景+ d& ~) @1 I6 b3 @
    1. 文件系统遍历(目录树结构)
      H, V7 ?% y" J1 Z- e2. 快速排序/归并排序算法
    3 y, `4 C- j% @7 ]3. 汉诺塔问题! L( `: J" ?0 M1 h: T8 a6 s
    4. 二叉树遍历(前序/中序/后序)
    , u' H* r/ }; z0 v6 ?. U3 @5. 生成所有可能的组合(回溯算法)3 ]6 u2 I6 ^: I5 w7 |
    / ^  b( N9 d2 r! ^2 l
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    3 小时前
  • 签到天数: 3251 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 J' z. G/ h: V% \, O我推理机的核心算法应该是二叉树遍历的变种。
    ! d5 d$ K9 O- Y. i+ {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; p, o* ]6 Z1 ^  E
    Key Idea of Recursion& y; T; T, Y: G3 F8 O, v
    % s; o1 D* j: ~) O8 n' O4 W$ y  n! |
    A recursive function solves a problem by:
    4 I2 J8 _$ e- e; Z* u
    $ [) l# `( j, r6 S+ v( i- v" ^# j" G    Breaking the problem into smaller instances of the same problem.' v+ l$ z7 |4 _7 {3 e# F+ H: S- ?
    9 ~9 C. V1 A4 Z$ ^3 r6 n
        Solving the smallest instance directly (base case).) T0 a3 g. r: o/ g1 H8 e1 n

      M7 M& p3 x3 g$ s) A6 `4 |6 s    Combining the results of smaller instances to solve the larger problem.% u1 k/ z" U$ [6 ?6 p

    ) [* H. V5 k! Q8 UComponents of a Recursive Function
    1 \) U, u8 K) |# z) U, M" G$ D. b6 }; _, Z* ^) @
        Base Case:
    8 Y+ Y' z* u, y7 @$ T' Y; Z& Q) J- H& F) ^8 k4 ]
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 _9 x/ n6 x5 g! W, b: Z0 h5 [! R

    % ]3 n  D+ z3 C7 x7 c. L* `        It acts as the stopping condition to prevent infinite recursion.$ x6 k+ Q% Q: F0 C" U

    , d4 G. Y9 E) Y/ L- [' f- Z        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 ]9 D4 Y" n: s# Z. m
    - _9 J, N6 Y1 E% _' }    Recursive Case:
    7 U" ]1 R1 \; F
    8 B8 i1 }( H8 q        This is where the function calls itself with a smaller or simpler version of the problem.8 H: `& H4 w  y& m6 b" l

    " i" l3 |1 b. y        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ' R+ a: o( F$ _4 Y$ R% Z9 @0 ]1 [: o! s' V4 \+ r
    Example: Factorial Calculation$ {' D# E# j2 ^+ l7 N+ k$ L
    7 O5 p1 D  G0 T6 ]
    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:
    ( P8 C1 S+ s9 u8 P; I9 H' {, g& g9 ?5 u( n
        Base case: 0! = 1
    ' [/ o6 W: o, p$ J
    + ^! |: E6 |% ]* y4 u1 a* [    Recursive case: n! = n * (n-1)!8 q4 \0 x3 w, r' j
    * E  A9 g1 G  j& E" B8 Z
    Here’s how it looks in code (Python):0 }7 C% x9 n( ]. ~4 t
    python: z% p1 K  U2 h4 J, h

    7 L0 [- A% a7 S/ P. ^, X/ G" t7 |$ P/ [( H9 J' s
    def factorial(n):5 Z" J. N  X5 x6 K9 _
        # Base case' c. Z6 [) g1 y7 I8 B/ S
        if n == 0:
    , B1 U  r1 w. Y/ `8 {        return 1
    5 {! Q3 p2 Q6 a    # Recursive case
    % K! x9 @( A1 ~- f/ ?    else:
    3 M( D' l1 X) X        return n * factorial(n - 1)& Q( X# s8 G+ M6 _2 ~" O
    / e, R. ?/ x+ t
    # Example usage) c! l+ C) Z, a) T1 B0 V
    print(factorial(5))  # Output: 120
    8 r" W8 q: q& U; _8 o$ s9 C# D  U# |$ y! _
    How Recursion Works) i; U) M5 Y# ]9 k

    . S8 [2 R6 K2 K1 L% e6 A% K% e4 M    The function keeps calling itself with smaller inputs until it reaches the base case.0 ~- M. P) N8 ?

    ( u5 l8 P% b  \, o8 |1 e    Once the base case is reached, the function starts returning values back up the call stack.
    8 e$ \+ S6 x1 N9 j. b
    % B+ X; A, r/ w3 j5 e! h    These returned values are combined to produce the final result.
    * e' }  I0 Q4 N
    7 r, M; A# Q2 }- T+ A$ TFor factorial(5):
      B! p: T5 N: A0 ]$ N3 _3 c$ H" p$ |
    2 D0 j& F2 S" X+ U6 U& A3 s- M6 B( Y) ?1 ~; ~! b$ I1 A
    factorial(5) = 5 * factorial(4)
    ( m8 ?# W6 X/ \* U+ Cfactorial(4) = 4 * factorial(3)9 R5 ]! X0 Q8 Z' w/ W
    factorial(3) = 3 * factorial(2)/ Y. e: @( B8 ]2 C9 a
    factorial(2) = 2 * factorial(1)- h8 Q5 [$ G- B
    factorial(1) = 1 * factorial(0)
    - w2 K1 j1 _+ K8 G' xfactorial(0) = 1  # Base case. ^. Z* N! f# }+ L4 @
    9 K# h. U* T% E4 F& W. Q
    Then, the results are combined:5 ^7 m2 l- J3 f0 d2 V* d2 s
    # p& _: Z: a! M! [' C5 q+ A
    6 m- k$ }8 r+ P; W
    factorial(1) = 1 * 1 = 1
    - _1 M% J: V5 C0 Z+ o  q, z; h" S* Q8 nfactorial(2) = 2 * 1 = 2
    4 y# F0 ]; k) s- ]5 bfactorial(3) = 3 * 2 = 6
    4 Q5 k" e, v6 j% l  sfactorial(4) = 4 * 6 = 24
    7 O# Z% J4 H/ K- }$ j0 Mfactorial(5) = 5 * 24 = 1200 @, i6 W' S: ?) `

    / `' O/ V. ~+ V4 E: AAdvantages of Recursion6 F, ?; c8 K2 W+ P5 e- G

    5 {0 t2 L: O) i" K    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).5 D* }. O8 \7 W
    9 ^; j( g* }, }* N* A) E+ n) I2 \. V
        Readability: Recursive code can be more readable and concise compared to iterative solutions./ m+ s# ]8 a! ^/ m

    0 j) ^4 @- c# E+ Y2 s+ p- X2 W5 K: kDisadvantages of Recursion* m7 ?6 \& G+ \
    3 @' r' r( }) J3 W; f
        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.
    : A; L* c5 _$ m. ?) _
    . K5 |3 I" V" G* Z8 X    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).% U4 p& p; A+ S; v

    9 q5 f4 G2 W7 Q( N5 G" |When to Use Recursion
    7 X, I- H: v  Q/ d5 b3 R: K; ]. u+ I' q( N, e$ t. Z) }4 r
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    : N4 |/ @# `9 ?8 M3 J. w1 O
    ; S- A2 H: i6 i) q/ S    Problems with a clear base case and recursive case.3 [5 ]7 K: {. x7 N; g1 M/ x7 T
    + d3 F7 l: _( H; w
    Example: Fibonacci Sequence: t7 ?0 [2 S7 |6 J

    1 w" O0 e2 e1 Z6 q0 [! iThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. f6 n+ {  c9 |: X
    # q6 i3 l" X: s5 b( g- n9 A, R3 N
        Base case: fib(0) = 0, fib(1) = 1$ I; @/ [* ~. k7 h+ f

    ; A" y7 r8 r' J9 K' o5 C* w    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    8 [; o: h+ S$ C/ w5 S. p- z
    7 T% @" a0 w- e6 O$ mpython
    2 B# ?  Z0 o. R9 d$ U3 l. _0 T& K. ^7 C; T) P
    7 {' T1 ]( ], K* T
    def fibonacci(n):
    - y4 |2 I/ T$ w- S) t  `+ w    # Base cases
    ( b4 g. k( q6 X6 n8 G    if n == 0:( d2 j2 \+ a1 j4 l6 w
            return 0
    ( m% [! l$ m! P4 r5 c6 [  q5 N    elif n == 1:: [0 [  Y- g5 `2 i$ G
            return 18 z6 r7 B7 u% @9 _1 {+ n
        # Recursive case& H4 C  l* ^. @
        else:. ^2 u$ N/ i- d" ~$ L2 y7 Q* c
            return fibonacci(n - 1) + fibonacci(n - 2)
    * D- R. T3 M" D5 [( i6 x; x' p; K$ z& w# F8 _- j  L. v- j
    # Example usage
    # y! i  F. o6 {0 A/ pprint(fibonacci(6))  # Output: 8
    $ N' i7 I4 G" P6 S' c- ^7 n8 x0 |8 o" q* ]2 ~9 K/ t
    Tail Recursion- I$ Q( W) u- c) U: [

    , V+ G+ q  n1 p- bTail 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).: X' X9 f$ i9 D. \

    ; B. X4 j/ s& n. V- xIn 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-5-27 10:40 , Processed in 0.072705 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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