设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + {# v/ Y$ l: \, S3 e' K6 i

    4 n" k' I* G- z# X( a0 }. V解释的不错/ H( H- d* H+ B

    : h: G. W% ?9 ?- z/ P. I* e递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
      I) r/ [6 D+ j3 T; T! r8 [8 T7 Z9 @- R) Y# [- j# W5 x4 H# v
    关键要素2 J7 i7 R' ?/ K" s' r- J% e4 ?
    1. **基线条件(Base Case)**
    * K: @* \2 Y& K) \* V. l, j- @) s   - 递归终止的条件,防止无限循环
    + g" j* T6 W3 y1 l4 h3 Z0 x   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1* ~% s0 \# N: z7 S
    + K+ M  c2 m8 Z- K7 o
    2. **递归条件(Recursive Case)**
    0 S2 `1 b4 b3 B6 B% f. u   - 将原问题分解为更小的子问题
    6 c( P8 F/ g6 `   - 例如:n! = n × (n-1)!
    - ~4 m" C5 o5 R+ o- V$ t5 r" V) N* Y
    经典示例:计算阶乘' q! H7 T7 Y5 M# S9 f
    python* @! S5 n* ^$ V3 B4 N5 g
    def factorial(n):
    ) \  f; \! i0 ]7 ?3 v    if n == 0:        # 基线条件- B& B4 Q) ~! w: [# F! Z
            return 16 }. z1 b" Q+ ]2 P5 d$ ?) c1 g
        else:             # 递归条件" L' G# Q; B9 w/ N- H$ F
            return n * factorial(n-1)# P! {- a* i  P1 O. _- d3 C6 q
    执行过程(以计算 3! 为例):- b6 V3 }" a) E* ]+ D2 N- j6 r* a+ ^
    factorial(3). N6 w& ]* ]' r1 I' d% O/ C. r  j; S* Z
    3 * factorial(2); j6 M3 b, H7 Q- Q$ y4 W
    3 * (2 * factorial(1))# d' I$ M4 V2 y1 W% \
    3 * (2 * (1 * factorial(0)))
    , |8 e& s+ b% A4 b3 F3 * (2 * (1 * 1)) = 63 ?0 ~8 m3 S4 j* k' `6 C! q

    # n$ y# s: u3 v& O9 X0 V3 E 递归思维要点( ]( r( D1 W0 |  W' X7 `1 Y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑  u/ r7 l* G3 u9 A2 N4 H* ~# u
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    0 n/ I% M& q8 G3. **递推过程**:不断向下分解问题(递)& Z" @4 b, v1 J) V
    4. **回溯过程**:组合子问题结果返回(归): h1 T5 _3 v: i
    " n; L7 r' Z/ W0 b6 G  l% |
    注意事项
    $ L$ ?$ E# s" N4 v8 y+ H必须要有终止条件
    1 n4 J5 Q) Z# h2 B8 Y3 I3 O递归深度过大可能导致栈溢出(Python默认递归深度约1000层)6 z: P" c5 S* r2 j2 _8 O  z6 I+ J
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    # ^8 V# }5 {' i尾递归优化可以提升效率(但Python不支持)
    ; G; _$ }8 T: ~0 H5 u. }9 m( O/ e6 |) u0 A
    递归 vs 迭代
    ! A% L. @4 k  i3 C& h: Y: G5 P0 U|          | 递归                          | 迭代               |1 Q8 {2 |, g4 {- [1 E- P
    |----------|-----------------------------|------------------|$ O/ C8 b" `- l' x& q
    | 实现方式    | 函数自调用                        | 循环结构            |% V/ u" U7 P, E% ^1 P
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 J6 h4 `2 F7 i5 Q* u5 Q+ c* R4 m| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 j3 b- ]7 L. @  a$ G& U) N| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 W2 O3 q1 R* V* r' D/ [+ Q8 x2 V" I8 ?
    经典递归应用场景" `9 D# M  n( a4 A
    1. 文件系统遍历(目录树结构)0 I) Z' y+ d* N/ ^3 h1 \
    2. 快速排序/归并排序算法$ L: f) Q, L! _" y/ K7 j
    3. 汉诺塔问题5 o% Z$ [* _  ~2 h& n4 _
    4. 二叉树遍历(前序/中序/后序)
    . f9 S2 Y- t+ F) w" O8 S9 y5. 生成所有可能的组合(回溯算法), t1 e" a- ]% i' K0 A

    7 G% C9 U# f- j3 n0 R试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    半小时前
  • 签到天数: 3200 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    1 o" |  n' S6 ^4 w我推理机的核心算法应该是二叉树遍历的变种。; V: Y$ A) c: _: g0 Q0 y, z* p7 G
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    $ [6 G" G* I! U/ z! c, k6 j3 r2 S9 x, P4 qKey Idea of Recursion
    " T1 ^& }: _- R1 q# C1 Z8 M9 f
    8 z- y  t6 ?3 ?( h* e% W" i1 fA recursive function solves a problem by:
    $ e1 V' b7 c) V9 l1 u  `9 c  f$ I
    & F' B( ^4 u( D8 n5 G+ h    Breaking the problem into smaller instances of the same problem.
    5 z; X8 _( I: {& z  l2 [! r9 _; f$ J4 W2 s) X
        Solving the smallest instance directly (base case).
    . i- R! i2 m4 H" ]8 |  E  D  M# ]9 c2 g2 i$ |
        Combining the results of smaller instances to solve the larger problem.
    8 N! |. X5 d: B; @$ Q! T6 X% J) @3 _3 n6 G0 J0 r
    Components of a Recursive Function  [9 y7 b/ |# n1 ?# p3 {$ B
    5 ^) {: C/ ]  `+ x' @$ f5 t
        Base Case:  k- b$ b4 `% N7 h

    8 p  h; n9 u' f; o- M) {        This is the simplest, smallest instance of the problem that can be solved directly without further recursion., e. c% g  P  Q; c/ R
    3 f6 N, \5 e- O: @1 m
            It acts as the stopping condition to prevent infinite recursion.
    , ~& r1 T; P4 P2 H7 c" g( y+ c; d8 C+ H  ^- k. k7 O8 H! F
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * T5 l: q7 \0 W) M7 C( ?  ^7 g5 ~! l* @1 Z& V1 X
        Recursive Case:, f2 E, n& C; J1 p- ]$ D) e/ z- R

    7 B+ C; q" h  C: A  f3 `8 v        This is where the function calls itself with a smaller or simpler version of the problem.( r8 F  l# }$ _' ~) s
    : g5 q) D, r  t0 \
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ) i) `9 D7 {; S4 i3 z5 c! M* M- [. Z' w$ A9 ~
    Example: Factorial Calculation
    7 p' }* A4 k0 ]( w, E  a" P5 X# J4 Z4 J
    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:
    ( q! Q7 b0 L4 J) H' ~" x# f# U
    # \: g" r, T+ d) Q9 M! V& M; A    Base case: 0! = 1
    % D1 o$ P, E8 {! W3 {: K' x" q% z, B$ Q( \% P0 Q
        Recursive case: n! = n * (n-1)!# w% m7 L3 w4 k8 }3 \6 c
    : Y; N5 Z: t8 S' \- X5 @
    Here’s how it looks in code (Python):- H6 ?# k2 u- Y! V  o
    python+ P2 N8 G. P$ J3 x9 y' V
    % i8 n$ q/ Q- ?3 J) ~

    ; j, [1 a8 g- xdef factorial(n):$ L) Q: ~4 q3 g% D( {& p
        # Base case% m8 ?( q( e% {2 J9 [6 ]
        if n == 0:+ ?) H8 W: }) m
            return 1
    1 t: y* _. r) ~# ?- B5 ~    # Recursive case" C0 s/ D/ h+ A
        else:
    1 L4 _) P* v" P        return n * factorial(n - 1)
    6 V0 _5 b' m. L7 {) y3 _0 A# U5 ~1 P$ Y% O# u2 S5 Z' c
    # Example usage
    4 p9 T3 p9 ]1 B& zprint(factorial(5))  # Output: 120# K& B* k$ Q$ P1 u% W8 @: q9 W

    5 Z- ^1 |1 C# j5 @How Recursion Works
    % }( w' r3 I. F3 a! l4 s# P) B4 p- a
    $ Y+ k$ V5 n3 q2 E$ \" K* u5 [    The function keeps calling itself with smaller inputs until it reaches the base case.* q" X+ l  C( @- Z4 p

    5 P2 m( L0 I3 w# b# l    Once the base case is reached, the function starts returning values back up the call stack.
    8 s+ \* N& N  h' ^
    8 z$ |' A1 f/ c6 \    These returned values are combined to produce the final result.0 s  X/ v3 o/ K+ m

    8 b. q5 U' t5 i# v# @3 i4 _* ZFor factorial(5):
    ! \: f- j6 Z- A7 J3 ]2 @) z* ?: Y: d2 m

      M+ B  w: z0 G% D* q% J$ Zfactorial(5) = 5 * factorial(4)
    ' n1 U9 S/ E9 Y. v7 W8 nfactorial(4) = 4 * factorial(3)5 w  e* c& k" b# Q9 L( ?
    factorial(3) = 3 * factorial(2)) L; i1 ]+ d" F( R
    factorial(2) = 2 * factorial(1): c' K: g/ U  }
    factorial(1) = 1 * factorial(0)
    1 L3 U3 F7 i# B4 L6 V3 M! j, U" Ufactorial(0) = 1  # Base case
    5 ~( s/ s% T7 P* o; Y2 f! E+ R# q7 _& o1 @  _
    Then, the results are combined:% ^9 _! G1 \& n

    " X# Y# ^/ N1 H& ^8 y1 N9 i1 i3 `
    % N* l" {6 r1 _( C( j3 vfactorial(1) = 1 * 1 = 1
    ' \# Q4 o) A8 e2 p( Vfactorial(2) = 2 * 1 = 2
    * I) @7 I+ E% @0 x7 Xfactorial(3) = 3 * 2 = 6
    ! l. @% u0 t* q5 I) m7 S4 qfactorial(4) = 4 * 6 = 243 D, P/ t$ a; a5 w: M7 K
    factorial(5) = 5 * 24 = 120
    0 D/ N( K5 a+ |1 f, F' ]* b- J6 H: c: X1 {/ Y
    Advantages of Recursion
    1 u! |" `/ x0 M  O* w5 x
    $ V% F' C9 m" s% ?9 n- J2 e    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).% v7 o7 b4 o5 p$ d

    1 H$ E0 X, j  ^& M6 v( J    Readability: Recursive code can be more readable and concise compared to iterative solutions.& _) S. s, s) c4 Z7 X% z) g
    - d/ o/ \9 z3 r" ?# a! `
    Disadvantages of Recursion% A# `* A. m. N- t5 T3 @

    " I1 O6 v8 y+ l4 f5 ~! i    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.
    1 |! P. H) L$ w! `. G# G( W
    ; f% J2 d/ s9 u+ Z: b. M  W$ v7 `    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    " ~$ v, I- a/ k, D7 X( J! Z# N  j4 q2 z! i/ T/ ]
    When to Use Recursion' t: k+ _; R$ r5 @) r2 r

    8 i7 z4 s8 Z) W" p. k    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # O& N& ^. v9 c
    & E" B- x- r7 w+ p% ?8 O2 K    Problems with a clear base case and recursive case.
    ) ^8 Q3 g6 z/ A4 U( s7 G
    & m8 b3 M! m5 N2 J0 O! v# HExample: Fibonacci Sequence
    # ^* ]$ s" l" O9 o' E3 S4 x) k1 e  L7 W8 t/ h5 Y
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) H3 J7 ~7 Y: b. m" t' b6 l, k
    - v* S: V% h" l) o- P+ b& H* v
        Base case: fib(0) = 0, fib(1) = 11 C4 ^& p3 q9 o: ?
    : m5 b& O3 i' r% Z& n% v5 L8 k9 |
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ) n  _) x# a* N8 \
      M7 H# v: V( B4 E# u6 J/ gpython
    4 E3 H- Q# e- d. v
    7 u3 Y" ~! I& }* W2 T  v# G  ~; c" C# }1 V7 K% }
    def fibonacci(n):
    # K9 p% x+ P4 n    # Base cases! |" t' E9 k; V- E+ z; s
        if n == 0:; N) [( \) l" V# g
            return 0
    4 ^& e+ K8 A6 W  _    elif n == 1:
    " }0 Y; u6 i, w5 F# P; L        return 1
    6 W# n/ Z5 B7 [( x% Q2 p    # Recursive case
    + a4 j$ J# Q5 K3 a! _, G1 _    else:
    9 k$ m" h+ w- h3 `4 j' N# X        return fibonacci(n - 1) + fibonacci(n - 2)& ?! S' M1 @7 c0 c! P! c

    5 o. y; l% F8 P* q: l0 r( g; |# Example usage
    $ g/ u7 E9 f3 Q  r! h- c: `print(fibonacci(6))  # Output: 8# Q: Y) Y! Q* i. _7 N) C0 G* R
    1 {1 e# U$ j/ G" G+ i) d
    Tail Recursion
    0 T3 q' m4 @( ^2 w: z7 _' p5 W. U
    2 r, k1 O( v4 l8 v/ l  H, aTail 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).
    # w, V, c6 B* \3 ]2 j0 b1 A9 P2 U# N, t% v  ^7 f: y+ p: s/ h, u
    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-13 08:29 , Processed in 0.072294 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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