设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 0 J2 v, L0 t2 |& h. u, q1 `1 y' w1 x

    0 F% V8 Z3 y& I, x, Z/ H$ ]2 Y( P解释的不错
    : y, f" p+ m2 b# s$ b) C! H' l) Y- x; N  n3 ?* ~  v
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  r8 M' r' ]# J6 \3 M
    8 K9 M; g2 D& ?* a, r
    关键要素
    2 v1 |( b1 j4 g0 o; x* B1. **基线条件(Base Case)**! K5 g( J% D4 U6 e  k/ g7 @
       - 递归终止的条件,防止无限循环
    ' k. g# k, e3 ?- E2 L: ]   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    6 r2 x$ E- q" e6 [$ u
    " J2 y& z. `. E4 _$ U: ?" L+ v2. **递归条件(Recursive Case)**$ \' \+ q+ E& ]$ w
       - 将原问题分解为更小的子问题
    , O9 C* O+ |, \8 B' A3 r0 _   - 例如:n! = n × (n-1)!+ T3 W6 d& `0 }$ U: g% Z3 z& @
    * @9 q3 }) ?$ @6 ]$ w
    经典示例:计算阶乘
    ' B) f$ |, \* y( O) _! Tpython
    # G7 n$ \9 ?; }+ y; m1 s; Xdef factorial(n):
    6 i$ W' x) m; L9 j, A0 U    if n == 0:        # 基线条件: j8 F0 k* G- W  A: x5 p
            return 1
    7 s: F, k  j2 {5 d0 X! x" D    else:             # 递归条件' U- O- n3 G# V1 N/ w" I' r
            return n * factorial(n-1)' P; D1 K; O& K1 ~0 C. q
    执行过程(以计算 3! 为例):5 T6 W0 ^1 s7 G7 p2 Z6 C% H
    factorial(3): k6 h+ ^& |4 H% c
    3 * factorial(2): k4 w* a+ i3 s6 V* F
    3 * (2 * factorial(1))
    9 M5 ~+ H/ ~6 b+ z. a3 * (2 * (1 * factorial(0)))
    ! a- b) w1 h0 C; L3 D% h3 * (2 * (1 * 1)) = 6
    8 Y1 f1 l9 O* \3 x( n" S, Z
    / w4 f8 Y7 i9 i 递归思维要点1 l, }$ V! ^" r" j2 k1 ]& t2 n2 g
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    1 f7 v* |! R& ^8 n( Q5 N9 k  P2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " e# `/ |$ p0 y  l; s  d# \( n3. **递推过程**:不断向下分解问题(递)- c) t& b. V# p! n
    4. **回溯过程**:组合子问题结果返回(归)
    / u% k, _  v1 h! \0 t) x+ P' r. u- h$ x
    注意事项
    / j6 Y- I5 m# n/ G必须要有终止条件1 v6 o$ t$ j" h0 _3 L
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 E/ Z6 _7 W! h) }8 O% X. q0 Q某些问题用递归更直观(如树遍历),但效率可能不如迭代- t  l) K+ V# H3 ~, ^; s8 }+ U% t
    尾递归优化可以提升效率(但Python不支持)
      L, |! I' a9 ?  E
    $ t* Y" {8 ^/ j( e( L 递归 vs 迭代7 @5 s- e' u* h7 x) g
    |          | 递归                          | 迭代               |$ B9 K1 i: f. y: r- F+ O9 X! o) ~$ D% h
    |----------|-----------------------------|------------------|
    " E9 Y0 _6 B) j* I. S2 x| 实现方式    | 函数自调用                        | 循环结构            |
    . _: P% f8 {( f5 {7 J6 J| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    # E$ i; T, Z3 N6 Q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- A5 Z( t6 B. u/ O  a
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 J, {& _1 c: p$ J, Z% D- I1 ^; o
    + ]- E! b7 R( F2 }$ o$ X
    经典递归应用场景
    & w$ F" d6 J' O5 _, U4 ~; w/ C- X# l1. 文件系统遍历(目录树结构). R  W& q6 `* i  z2 T9 V' a
    2. 快速排序/归并排序算法1 h0 J) }, r: c6 ?  L! `5 |
    3. 汉诺塔问题
    7 U, d( ]' \4 l3 H$ N4. 二叉树遍历(前序/中序/后序)& {8 A9 B6 `0 V( d$ G
    5. 生成所有可能的组合(回溯算法)
    / U" t: @' G4 P" Y. L7 z8 A# l  e$ r  S7 D
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    3 小时前
  • 签到天数: 3119 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, P8 v2 I$ |7 R" d- D( G3 z$ W
    我推理机的核心算法应该是二叉树遍历的变种。
    7 s3 `# e9 b. e& c  n# q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 r! l* Y" o: N' oKey Idea of Recursion
    1 p, N- e8 @: t
    ! p8 U4 X. m3 q2 P" B0 hA recursive function solves a problem by:* I+ w/ Q5 \5 A; t1 a) z$ f1 S- Z
    5 e- R" C. w* W+ K7 o+ Y
        Breaking the problem into smaller instances of the same problem.1 v$ g9 ]9 P! P# Y) z
    " m  L& u) N( Y; @. u  e) Y
        Solving the smallest instance directly (base case).
    8 M5 \' \* H& n7 M, s) p7 L9 y
    # c& E" V% ^; w9 l    Combining the results of smaller instances to solve the larger problem.# ~) q* t; v% d6 j$ x
    . l2 Y: _5 b& X9 @; g( P0 M
    Components of a Recursive Function
    - k: c" v6 K4 k* T9 o" c! s+ r; a$ ]: e
        Base Case:
    / `" j7 R( w1 u. G& u
      |$ ^# m  ]3 x9 F, b2 ^        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 v, G, I$ R) R' f) j. s( ^# K6 O. t; d% M; W( M9 l
            It acts as the stopping condition to prevent infinite recursion.
    / |5 I% c) w7 H
    4 R+ r& X9 \% [9 r4 D        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.8 h$ H6 V9 j" K7 |/ g  P

    / g& Q& |! Y. l: C0 ^$ w$ v    Recursive Case:$ V4 M- J& H% E; W; V7 O

    6 ~0 D% Q  ?9 g9 S6 l        This is where the function calls itself with a smaller or simpler version of the problem.
    : V8 A6 n3 R$ }1 L/ a$ y1 M6 e% X2 y# [0 j! o- I
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).0 q$ d/ y; a# j9 x  j
    ; t) E$ H) z% G5 N4 F
    Example: Factorial Calculation9 L4 I) W  f* w: z

    3 m9 e0 [# `, w" |1 bThe 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:  s2 |+ ]  S  s0 D& s

    # h2 A; _' f* e' p, G6 @% c5 |    Base case: 0! = 1" t/ N+ P& I# I- d! Y  H. X
    0 A2 }: K+ Z6 g1 [4 h6 X
        Recursive case: n! = n * (n-1)!/ J" Y' H/ Q6 k# t( i% \- W
    & c1 A* N7 ], d
    Here’s how it looks in code (Python):
    # L% O$ U/ }. d7 Vpython
    $ l; T% U3 z/ x7 k/ [
    1 q% o0 Y& m9 P# F* p' b) \3 Z& H( D6 Y
    def factorial(n):. G1 w' O6 r2 C3 C
        # Base case1 N7 v4 Q3 q! I  ]5 t7 `/ t7 {
        if n == 0:
    / }4 D6 ^; x0 x* j& S4 L        return 1
    & O; F/ N; j8 X+ L8 A0 L) T) m  c0 k  y    # Recursive case' b0 e# p+ l5 ~, \3 S
        else:" C5 l# B0 F3 Y7 J& Y$ H6 Z
            return n * factorial(n - 1)
    $ L$ d2 g+ ^! T" I$ U( }0 t, t% K
    5 B+ H: M9 F" s' p8 M5 l# Example usage/ ?1 h) F' d% @" j: i
    print(factorial(5))  # Output: 120! y1 U  Z+ b  n& k5 K& U! }

    + \# H( I- M4 C1 o$ b% mHow Recursion Works' _# F1 B& R* K

    ; ^& R, A' V6 W7 }# I    The function keeps calling itself with smaller inputs until it reaches the base case.
    , X5 o  u7 X" l! y; c' P3 y
    8 u; c0 d) T. s. G0 e9 E6 h    Once the base case is reached, the function starts returning values back up the call stack.( ^; r3 ~1 u2 S8 Y
    # U4 }% H. d! y" E# B
        These returned values are combined to produce the final result.
    0 y' f7 n3 T5 W2 M
    * f1 D3 U4 B/ r7 R: BFor factorial(5):" M  d: v0 S4 `) M
    . Y/ l6 c) |( \+ Y) W. f3 A
    6 N  X5 j3 T, a
    factorial(5) = 5 * factorial(4)
    ! f4 R0 R, b- k; f- l0 {3 Sfactorial(4) = 4 * factorial(3)
    . O! `+ @9 z9 R* h# m# B! sfactorial(3) = 3 * factorial(2)
    " R# M" a5 k% Z2 `factorial(2) = 2 * factorial(1)+ o/ n' W6 B9 r0 N
    factorial(1) = 1 * factorial(0)0 b; _8 m8 O5 k* E+ `; L( D
    factorial(0) = 1  # Base case
    " D; }( H. H/ h: ~0 H& v
    2 m- B- ^/ ~- i9 M% vThen, the results are combined:. F2 Q) M- X# L8 c* ^; {8 |
    0 Q7 y7 q0 K! m6 B* b0 o
    # f/ K. c: x0 w: i" k) u3 X. I/ v
    factorial(1) = 1 * 1 = 1
      J& F# U3 o( _4 @) m3 kfactorial(2) = 2 * 1 = 2- J/ g% a9 ~/ D
    factorial(3) = 3 * 2 = 6
    & Q1 I; p9 `- x+ Ifactorial(4) = 4 * 6 = 244 G! X( A- `! ?) s' M
    factorial(5) = 5 * 24 = 120
    ' n! c) K  \0 B8 l  O, z5 c6 \9 g
    + b9 X3 X- z+ n5 y9 ^. u0 m- ~2 w( zAdvantages of Recursion( d& i! ~9 T2 h6 Y" J: M
    ' J+ a' x" W1 ?* i4 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).: [  ^& _2 {2 g  z  ^, q3 P" l
    - ~; |1 l$ C: H) t7 }6 r9 p+ t
        Readability: Recursive code can be more readable and concise compared to iterative solutions.3 `1 a% O1 ?! @9 K4 V. \

    / ~" j5 J6 \! f& [6 tDisadvantages of Recursion
    : C9 D" f; j0 P# ]& _
    7 l( v& O: v4 g7 z' B) z& ~    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 o3 d, B5 P# k) n' l8 X! G, H# ^
    " _1 e) E6 L  f$ ^    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& v8 s% u1 m9 ^8 Y3 j7 a7 F

    - g1 p! P& j' S/ @( ^" _# zWhen to Use Recursion3 u) d( [, H$ I; X
    " ?( f$ x2 t- |8 S$ {
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( o$ _0 W2 C& c/ J
    3 J. W  a3 ~3 c+ [: u
        Problems with a clear base case and recursive case.5 S$ n3 t5 E" L
    ) i6 g: I, Z' m# F& O
    Example: Fibonacci Sequence
    6 s+ q. n* |8 i4 C6 T) u  _0 e, D3 Q* f  n0 I) E5 `
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& n4 K, e4 W! t6 i" W, n
    : e$ Q. J9 l" F' T0 M3 s) g
        Base case: fib(0) = 0, fib(1) = 1) J) v+ I! w6 j: `. I2 m, W3 r! e

    + e0 i0 N! o6 Z0 a) P    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    5 q, g* Q" x) G4 D$ X2 \3 N0 `! _, @/ @- T1 r
    python; `; P! p+ S2 V

    : n$ q7 `, T' E' K- K0 u$ v
    & |/ h) b  ~# C: sdef fibonacci(n):+ ^% H! u0 `* s1 }5 S# ~& g1 W
        # Base cases
    0 x8 q# Q3 q1 h    if n == 0:  s: E* M4 y  ^( g# b2 R
            return 0
    4 z' ?2 h4 }1 ~1 P    elif n == 1:
    " M* B. C0 `9 M0 l        return 1
    9 C2 j' w6 c2 k* Q. b    # Recursive case* `& Q* T( y' a
        else:
    ) c5 i: A6 w, D! ]        return fibonacci(n - 1) + fibonacci(n - 2)
    6 q/ U+ n2 u- c9 |' [$ p* A! M7 o5 H% Z
    # Example usage
    9 @" @$ _" y: L. ]' B" rprint(fibonacci(6))  # Output: 8* E6 S. d1 ~) \. Z9 ]- V: d

    1 i. [' v+ U* O% k; [& V( fTail Recursion! A# q0 D$ Z; g, N  [. L

    & v1 a+ Z$ p8 e  rTail 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).2 B. `/ {" E+ Z
    - S& j4 V# f* p3 @8 ~9 r
    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, 2025-12-16 10:52 , Processed in 0.030065 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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