设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! g& n5 g# o0 X$ s, u5 Y2 I
    # M! A: _7 L4 o0 m! W; \( h0 F解释的不错$ ~1 D+ B7 v6 J8 ]% D5 H
    & w  }- ~6 w) y* M$ [) ?1 R
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。9 P! O8 }- c  X$ J( h% g

    0 [5 I7 T3 r% o7 K 关键要素9 ?/ k3 i% ?9 p) n) r
    1. **基线条件(Base Case)**" @3 G' H$ F# s& ?8 L! ?
       - 递归终止的条件,防止无限循环/ r, C9 T& M( F1 O
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    6 s5 g& O+ a9 N& u  B- _! \( v, b0 l. k+ \) d: `: C
    2. **递归条件(Recursive Case)**
    7 D  p& q- h$ q, a+ h7 J   - 将原问题分解为更小的子问题
    " l  ?5 p' |6 V5 q. T   - 例如:n! = n × (n-1)!
    1 `3 C, \; A3 t& a8 }
    ! |3 h3 U# w8 K' w 经典示例:计算阶乘3 Y: V2 W) x3 J& i
    python
    1 f$ W# j4 y& z0 X# a5 ?3 mdef factorial(n):
    ' g; k9 L! P; ?    if n == 0:        # 基线条件
    8 W( ^; Z+ }6 b' _        return 1, ~9 c# k& ^+ o: |  I" Z2 Q
        else:             # 递归条件% e+ f2 F, H" h' Q+ y' ]7 x
            return n * factorial(n-1)0 i$ Q& ^: x. e2 S0 u( U& y
    执行过程(以计算 3! 为例):
    8 z% K" O% N. X2 s0 v$ Jfactorial(3)/ b/ P" g1 S. D, t$ @! e( `
    3 * factorial(2)
    ! z3 M' r* b) v+ k/ Y. J  I% {3 * (2 * factorial(1))4 V' \. [; P- q& C5 |6 m# k
    3 * (2 * (1 * factorial(0)))+ j3 J( m1 p& E! @; C9 U1 o8 p
    3 * (2 * (1 * 1)) = 6: i' ^# a8 h' B: b3 E
    9 e) j& s: Z( n/ e5 l+ K8 F' A
    递归思维要点
    ' a0 ?& ?" \6 a2 u1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 ]  m! E3 ]% U+ r2. **栈结构**:每次调用都会创建新的栈帧(内存空间)  ~/ q: y+ D3 n" |: j% i& v
    3. **递推过程**:不断向下分解问题(递)
      t0 k6 e1 ^0 z6 g# u# ?! z4. **回溯过程**:组合子问题结果返回(归)
    $ ~7 p9 o7 l1 u" E% b8 S
      u/ o9 T: t/ C1 k3 s2 | 注意事项" k  v8 u6 X. x# Q3 x: y5 \+ r
    必须要有终止条件  |. z! {  ?9 a. L( a- s" T
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层), I/ ~" _  w* i3 o) R9 F# {% Z4 m
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    6 d! X' O. a, X- W: N: j尾递归优化可以提升效率(但Python不支持)
    - K7 e  q1 G6 @. I, w# t
    : i! A* q5 X5 r  F, x( T 递归 vs 迭代) }9 @0 n0 r' |1 f9 u
    |          | 递归                          | 迭代               |
    8 t: ~" h! P* q! A|----------|-----------------------------|------------------|, A- p& l' Q: w% ~# l
    | 实现方式    | 函数自调用                        | 循环结构            |
    & d1 I$ U* W0 k6 m8 O| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |4 l. ~* D$ v* s% G+ ]# Y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% g. j# \7 g! B- U. g6 @$ ?+ Y
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    " o. _" l7 x4 \5 u2 n1 _& ?' `. D5 d2 ?. {* a. e) G
    经典递归应用场景  Q% G1 B4 ?8 O$ N7 V" c2 Z
    1. 文件系统遍历(目录树结构)
    $ P  E* @" E! J4 F- w2 |% u2. 快速排序/归并排序算法9 o4 \6 D6 z$ _$ C. N% E8 V
    3. 汉诺塔问题
    % M9 R/ J# H0 \, P1 ~$ U1 k4. 二叉树遍历(前序/中序/后序)9 B$ {( R, [! [3 y" l$ c; M7 o
    5. 生成所有可能的组合(回溯算法)5 i" Z. `+ }$ {

    , \$ k* L' s) }' V9 m0 K, o. x5 E试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ l. n* z- K2 l/ Q
    我推理机的核心算法应该是二叉树遍历的变种。, z6 A0 I, g* R8 F7 e
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, [) L; [4 |2 {4 Z
    Key Idea of Recursion
    1 {: o' F9 \4 W. z: i4 G6 j" W2 j7 o; u8 l
    A recursive function solves a problem by:3 f; B  |# V, Y5 l
    / Q8 u5 d: |( C8 [$ w# A
        Breaking the problem into smaller instances of the same problem.9 s5 p  |7 y( y& ]9 |
    ; ]9 p, }) A& i! m4 ?7 ~) Q
        Solving the smallest instance directly (base case).
    - c! E+ {  i* [8 V9 h, a5 A. @
    " l, N# T$ k5 R; t    Combining the results of smaller instances to solve the larger problem.( [, A: r6 B: v5 q* t" p
    5 ?) Z' b) L6 s- p
    Components of a Recursive Function8 d, N: f5 m9 n2 f, h' M: e. N  L

    & V5 }' m- F' R! w+ g$ C    Base Case:  m! c) Z& |2 o& K% S! Z( h) X
      {" I0 D- O0 R" P
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    . J% D6 y  R! |
    6 k( t" Y, ~. p, L5 \1 i" [- a# p        It acts as the stopping condition to prevent infinite recursion.2 B  \4 B4 K& d$ k2 D

    9 Y& g) b& ^# \% V* X/ r        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    0 t7 K; I; |4 e+ _" J6 ~6 i" Z! R- @) G& l5 R& }
        Recursive Case:, v3 h6 L7 x+ k& f/ n
    " w  q! E2 e* Y2 z9 Y
            This is where the function calls itself with a smaller or simpler version of the problem.- d6 e9 r5 b- p$ J  S

    . N% h+ f; H. A, {# J* y" W. K        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 F- X3 X9 Z1 B$ F% P- T2 j4 I* G$ o2 J$ k% Y: h% q- E4 n$ S
    Example: Factorial Calculation
    ) k: z# `* j8 m1 Q* g" q1 b
    - q$ H0 x) x$ I+ c5 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:
      d4 G+ F9 c2 O) c0 `2 f8 H. U
    0 {2 l: m. q9 G    Base case: 0! = 14 ?9 o6 z% t9 u9 L7 [% i" q
    ) ?3 u- q* J8 B3 q$ z
        Recursive case: n! = n * (n-1)!
    8 w' q; j. l) X
    # r% b+ m+ C  V0 |/ n  u" NHere’s how it looks in code (Python):+ s% N2 z1 Z8 ~4 G
    python
    0 b  \/ F7 b0 u; \$ Y1 ^) E: U) ]: n; D& f
    $ ^0 I) V: L0 r  s
    def factorial(n):
    1 \) e0 _3 b$ D/ b5 ]* h/ r" y+ I    # Base case
    & a$ _" J+ Y5 @% I0 T    if n == 0:
    , C$ R- c  g& ^        return 1. u" i& k5 H$ P- B
        # Recursive case" Z0 e& ]+ ^: X+ Q: {' d
        else:2 [. s  @  o; I8 z
            return n * factorial(n - 1)
    9 S8 P7 w% V- v/ a7 S
    7 v0 q8 _/ O+ f5 Q3 Z8 R, o& R5 s# Example usage1 B# y" \# p! [+ G
    print(factorial(5))  # Output: 120
    6 |, d9 X; V6 ^+ B4 _6 p  ~' v2 H8 s9 D- H) U  Z) [" N
    How Recursion Works
    1 S* l* s6 C: f5 ?  U' Y  D/ i+ x2 M
    & D% q) z- @9 P$ D/ T6 u1 p    The function keeps calling itself with smaller inputs until it reaches the base case.
    9 _# @7 d' {; r" K. S; K2 X3 O- F9 H5 L2 t
        Once the base case is reached, the function starts returning values back up the call stack.
    & p9 v, U; T" G, s' c' v: \
    5 K, z) w0 E* w; |- \    These returned values are combined to produce the final result.
    6 I* T% V2 M' S# q7 u& l3 ]+ j
    ; s: x. I8 ~3 w/ kFor factorial(5):3 w3 O* ^" ~7 M  |  W; F4 F, X7 l
    9 Z1 `. d; \  ]4 A
    8 N2 n4 @" @' Q% F  n  O5 S
    factorial(5) = 5 * factorial(4)
    - z, s" A. u2 \; {1 b0 r$ @2 w' Sfactorial(4) = 4 * factorial(3)" ~! |! ?2 C/ i" l; s5 |2 m$ E# b
    factorial(3) = 3 * factorial(2)
    1 s8 A' C, c& L8 P: L( Tfactorial(2) = 2 * factorial(1)- a1 `1 C; ?! H& ^
    factorial(1) = 1 * factorial(0)* `" y7 F1 E5 |/ ]. Z& D/ h
    factorial(0) = 1  # Base case/ A% z3 e: L+ @/ r6 \
    ! o% r& T5 [, ]& u( F% M' F9 G; Z4 l
    Then, the results are combined:
    4 I* R+ B. y, E' D8 z2 u8 t/ ]% d  n/ @8 R$ e; j# A7 c
    & l. O8 ]0 h# C" O& d
    factorial(1) = 1 * 1 = 1
    % W* {3 E. P9 b& R* sfactorial(2) = 2 * 1 = 2) k8 E' p3 Z" J$ J+ {$ d, y
    factorial(3) = 3 * 2 = 6* Q7 ?" {, Z0 K
    factorial(4) = 4 * 6 = 24
    : p8 ?: L# }3 e  P, pfactorial(5) = 5 * 24 = 120
    3 i- n1 f1 P+ b& |; u
    + o3 J. J3 y- [- u& u4 H+ M7 oAdvantages of Recursion
    5 C/ d$ x& i6 o3 g; V) M+ f9 i& b6 g& B
        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).
    1 s7 u  b2 ]' B4 m7 ]# d0 v' N  s
    6 ^( A/ L/ B( c; p- v/ A- M3 w: R    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    5 {1 e3 J; _# d! B
    & q, {# j$ C( }$ U5 H& S' M0 KDisadvantages of Recursion
    " F( U! f2 r4 u# M
    + `, c& n5 d# G2 ]    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./ o7 K3 X2 @1 Y) u/ \" O
    / B& q; G; B. I# C
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    5 }8 C) r1 O  m, K. ]
    1 {/ ?3 y" x# [% {- {; o* }0 LWhen to Use Recursion7 A; q0 l1 w9 T8 n4 k0 D! m
    7 L! b9 y. t- H2 z7 |
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 C4 G+ a: ]+ e: }$ ]( b& b- \9 Y7 y: s; J! }" L. Y
        Problems with a clear base case and recursive case.1 C; r0 H! m; J0 x

    # E" ^0 V8 O. P2 ZExample: Fibonacci Sequence
    , }9 Z7 H( D& ^7 V0 p' q& `
    ' Z  G7 r6 {+ ^9 vThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    2 V. V: P/ O8 i! f, ]% V0 b5 ~& \5 b* O8 Y: S6 N0 _
        Base case: fib(0) = 0, fib(1) = 1
    ) y$ S# [2 J+ t+ S' ~+ N; C& c2 l# K$ d2 ]; _
        Recursive case: fib(n) = fib(n-1) + fib(n-2)2 T2 ~# ^. `4 C, C& M8 y
    $ T. c+ |, ^. s2 A+ ^' R7 U( [( Y1 S
    python+ A. b4 m, R# h9 ?; ^+ R! D

    4 J$ m3 C( o) R( v. A7 H
    % d. }8 s$ }, c, b1 p7 idef fibonacci(n):( o% w8 d: [- |4 X2 e# b3 M( Q& F
        # Base cases, i3 u8 e3 k  ]# x9 i
        if n == 0:
    8 p* u' n6 p, ]) s7 G( _" q        return 0
    $ E7 D. _0 x4 H, T& t/ @1 F% n    elif n == 1:* _: p& S2 r- \  n  C
            return 1
    ; p- p8 r3 o; K1 `    # Recursive case0 |8 X! `6 i, h1 {" c( W$ l. ^
        else:, ^6 e' U9 T1 v5 O) F! F  I, h
            return fibonacci(n - 1) + fibonacci(n - 2)! s* r8 l6 `+ h" u
    + A7 n; f1 m7 |2 k6 |& a* G
    # Example usage0 }0 B6 M- H* }8 g# o2 L* {
    print(fibonacci(6))  # Output: 84 V6 \) @( d/ I$ ]) q

    ! @) ^; i- S0 s* ~9 b$ c5 iTail Recursion3 e' q& ^5 y! w

    5 }, H4 j* L, t" k# M; @5 g& LTail 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).6 K6 W4 L& G/ m3 x) w9 |- h4 @' Z
    5 I4 K' Y, M% e1 V7 X, t* b
    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-6-7 08:39 , Processed in 0.066261 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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