设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 Q7 _4 t9 T  ^

    & D# O6 Z- Q$ _' y$ c2 W1 q解释的不错3 |- L5 c& |/ X! c9 n

    ! z3 h7 @$ K: y7 \! V( S递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    + L/ D+ U2 Y7 U7 m3 L3 a
    9 u2 i6 c# @7 |1 d: i4 ]4 O 关键要素* H0 y6 q  J, _* k1 {# g7 J& {
    1. **基线条件(Base Case)**. o5 m" p/ |6 k0 J: R) Q, o
       - 递归终止的条件,防止无限循环& R5 j5 K4 t' H% t. d
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1- B7 K' s8 W4 s( O1 ?4 M

    ) y0 Q3 B# F+ w4 {* ?2 S6 U2. **递归条件(Recursive Case)**
    3 a5 \9 n5 F9 t9 c" e* y/ s  U   - 将原问题分解为更小的子问题9 T+ c( F) o8 r: b
       - 例如:n! = n × (n-1)!
    , Z- s, A, y6 b3 y
    . J2 Y3 _/ W: E 经典示例:计算阶乘) a% l5 }  ~7 `7 a: C& z: U
    python  {% T3 P/ Q% X6 K
    def factorial(n):1 X3 v' A/ P/ s! `) F1 m
        if n == 0:        # 基线条件2 S! y5 E" b& I% W4 P9 C
            return 1/ L- [, F; G5 p0 D4 n* I
        else:             # 递归条件" d! U6 j$ ]- p/ k
            return n * factorial(n-1); P7 H; S% z. R
    执行过程(以计算 3! 为例):
    ! C3 B3 p4 T4 E4 Lfactorial(3)( W2 E/ u! K2 P/ W
    3 * factorial(2)5 |3 D4 w, l$ ?2 d6 V' c* z2 K  |
    3 * (2 * factorial(1))0 K, X' m- Y$ }! l9 z6 d, X
    3 * (2 * (1 * factorial(0)))
    , a* l$ Q0 [% \8 P% V" x& N+ {3 * (2 * (1 * 1)) = 6/ s0 H+ U2 s$ v4 O& Y

    3 O8 I: i3 t, `) y4 s 递归思维要点
      d; q4 x+ h  O, `9 e+ N' h1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / f( u  u1 {- e9 ?8 T- [2. **栈结构**:每次调用都会创建新的栈帧(内存空间)1 @* `6 P' x* \* j/ k5 [& N0 V5 |+ w
    3. **递推过程**:不断向下分解问题(递)) n' E9 \" W" [' a; B- G* r" j  N
    4. **回溯过程**:组合子问题结果返回(归)
    3 d" |2 `1 W2 w) U
    ) X$ J: P! Q8 u$ a0 _ 注意事项
    $ l% p! h& N; i5 y必须要有终止条件$ L- u2 `& p5 l* u9 g
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & V0 E9 ?  }6 u2 N' b某些问题用递归更直观(如树遍历),但效率可能不如迭代
    / q) d! E  q+ x尾递归优化可以提升效率(但Python不支持)
    5 S# d* C) K5 o
    8 H4 V( s/ |$ w8 w* I 递归 vs 迭代
    * @& x, T1 g5 x! |6 @4 m' E|          | 递归                          | 迭代               |) K' H1 _- B6 V( d+ [" t
    |----------|-----------------------------|------------------|' w( O0 j# d: {( A
    | 实现方式    | 函数自调用                        | 循环结构            |
    6 Q7 g! i* w/ || 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    " G9 W7 |* x- u" X: y| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! [: K0 R0 l1 a3 K| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |' w+ Q1 @; E, J* b0 P7 z1 A" @( |
      V6 K* @# N: c+ t+ a
    经典递归应用场景& l6 o2 x" X2 ^7 B6 {8 H1 \6 ~
    1. 文件系统遍历(目录树结构)
    $ T( m4 x  K7 h8 R/ D: |* a$ T* M2. 快速排序/归并排序算法
    / X5 V" g0 n# G" x3 D/ C) R1 Q9 q3. 汉诺塔问题) a8 }% x  B) v" f7 L
    4. 二叉树遍历(前序/中序/后序)8 T- k5 k' z6 n5 W  ^1 n
    5. 生成所有可能的组合(回溯算法)
    ; z3 B* l: U0 J/ O6 {" c: h$ w/ z+ `: I
    * x) l8 _2 x8 p& j! X) W2 @试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 06:32
  • 签到天数: 3164 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( P- s. z$ A7 T) n* L6 V% W: k2 Q! e/ V
    我推理机的核心算法应该是二叉树遍历的变种。5 a, u) z$ [1 T) u* C9 w" n
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    - u; A2 p0 N  m6 F5 KKey Idea of Recursion" e' n8 D% x9 j+ s9 t
    0 o6 p1 K2 u% @, n' T4 X$ J, Q
    A recursive function solves a problem by:# F9 W7 r4 W# `, |
    / R' I( @4 F7 R8 p3 H' [) e& v
        Breaking the problem into smaller instances of the same problem.4 q5 B& m2 ?8 g0 u

    ( R: w1 `2 i: K4 `, `    Solving the smallest instance directly (base case).
    0 F2 U& Z. w; S0 N8 |" h" Q! A, l( ]/ I1 E) r
        Combining the results of smaller instances to solve the larger problem.% d( B4 u. y8 B& l+ i; Q$ r/ t
    7 t& U" I- P* _$ j
    Components of a Recursive Function$ n; ]  J- L% Z! r
    ( I$ }) c9 S7 J# o& @+ o+ H8 C
        Base Case:
    6 c, e3 ?# G) w& D
    ( L- ^2 h; {; u3 d        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    - p2 P6 \; P# ^+ D7 t- x, H5 n3 t; ^" z, L1 z1 S% k  m
            It acts as the stopping condition to prevent infinite recursion.9 q, q: P6 T7 N" z4 g! ^! G6 R3 h

    6 q- i5 i, k; M  y/ U- F4 w* y6 t        Example: In calculating the factorial of a number, the base case is factorial(0) = 1." ^" u5 U& _9 X) u( ?! K
    * P9 R3 X+ S+ o
        Recursive Case:
    0 D  W6 W; g  _, Q
    , s- x) V3 G* X& q& S        This is where the function calls itself with a smaller or simpler version of the problem.
    3 e0 u* Y6 ]* C5 Q6 `2 Q; ~. f! e) |/ D0 r# ]" c+ _
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).4 _( [6 h% K9 l' q

    % m0 L2 r, A- xExample: Factorial Calculation. t; ~! B/ P7 O. M, Z
    ! c8 Y; j) Y$ L, m7 a
    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 e" Q+ m# N  R" z: n& Z
    3 O# H+ ^/ S1 z
        Base case: 0! = 1
    0 O: U9 {8 D- T+ W; Y' H$ w! V  a4 d( y, R- K+ X
        Recursive case: n! = n * (n-1)!
      @! s6 `" C7 E$ _; E, T1 T7 X$ W+ ?) Y9 v
    Here’s how it looks in code (Python):$ y! e  W" }2 K3 O
    python
    6 Q$ v4 t0 O7 ?8 i
      D. {  X3 m/ o& t7 x! C& {
    + o' f- |. `( F4 i1 I: ?4 ~def factorial(n):
    5 K# ~6 e8 i4 C! \    # Base case3 w9 {) R9 r4 T) z. J
        if n == 0:
    7 I; h' h$ F4 U3 Y$ D/ Z- X9 L8 E        return 1" f6 b& F) b( e
        # Recursive case" q$ L( w! b5 f0 m% o
        else:
    , T1 h0 r0 K5 z% ?4 U1 S5 _        return n * factorial(n - 1)
    7 ]' M# A/ k- V' {5 A5 r
    ! S3 y7 }: L3 n) f# Example usage
    / D# U* b/ t# rprint(factorial(5))  # Output: 120$ f& z7 ]; F0 b  C9 J2 {* Z

    4 S" ]) Y4 U0 _# Z/ YHow Recursion Works# j$ r" k. {. T9 W% ~8 {9 k+ d

    4 F2 P6 J: J0 Q* t. k8 l2 `0 U( Q    The function keeps calling itself with smaller inputs until it reaches the base case.
    ( r, i' Y( l: O5 s: h  H
    - E: y2 I+ C  p+ ?" {% k: M8 p    Once the base case is reached, the function starts returning values back up the call stack.
    . T8 B9 ^& @" S- L, I% D; p$ `' U% Q
        These returned values are combined to produce the final result.
    0 M6 }  s; }3 J8 H
    5 t3 a3 \3 B' F& |/ H. XFor factorial(5):
    ; j6 K* f; \. Q% U3 Q4 m. `) M) l/ {$ Q& K1 n4 c  V: o/ D
    ! ^. A. C- l3 e' q* w
    factorial(5) = 5 * factorial(4)
    ! f  e0 _, z# v6 c7 c5 ~factorial(4) = 4 * factorial(3)1 ]  z0 f/ D& d' [/ t) p9 x
    factorial(3) = 3 * factorial(2)' y+ Y# I3 C$ j+ @: r( {
    factorial(2) = 2 * factorial(1)
    6 W6 ~3 a. V* l4 X  |factorial(1) = 1 * factorial(0)% D) @6 B8 |' Y* _3 ~+ U
    factorial(0) = 1  # Base case) M5 \. A! m1 s* k$ |

    " @% _4 e) m7 T8 ^8 U' E" XThen, the results are combined:
    / @& J- G9 Q1 p' P/ F& U! v: x2 ^; ?; ?. B
    ! t. M/ t& N. [1 V
    factorial(1) = 1 * 1 = 1
    * z( N) N1 U# R- I7 T. yfactorial(2) = 2 * 1 = 2& K6 K/ _% M- i/ j
    factorial(3) = 3 * 2 = 6, d- X# t' H3 h4 g5 |" r0 I
    factorial(4) = 4 * 6 = 24
    ( t- J6 [& x' [" ?/ k, Z9 @9 f# Jfactorial(5) = 5 * 24 = 1203 G" g/ v# H9 T' M+ O- y

    - I; T# w9 z' m1 r) q* sAdvantages of Recursion7 S$ E9 \9 g* _' u9 p4 Y: e
    9 |5 E- d* L0 {! |; n
        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).( k% U2 T" P7 l5 F

    . D2 _4 R4 h$ Y" H- {: @* z4 i. ?    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    4 l! [  e; d0 o0 C: i( w1 g& F4 Z' }$ @! Q! U2 v) t/ A6 M
    Disadvantages of Recursion
    1 d5 F1 `" n% x
    - m/ |, w3 C5 Q+ e: |  x    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.8 c% ?% F: M# J1 K8 l9 V
    2 b. _. K  E( T9 }/ Z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 a  j. X  d3 Z/ o: N

    6 y9 d8 t) {" L  \9 u1 d. FWhen to Use Recursion
    4 c3 D0 c0 F# s+ e' `4 D
    6 x6 y) |7 g" e! \# A) d4 L3 U    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    6 c: Z7 w' U( `  u* V
    . G1 w& k1 P% l' [2 V2 k6 w0 t* a  L    Problems with a clear base case and recursive case.
    ) m. |) ?$ ]$ b" R' x. Q( x
    % m/ A; x0 p" n- ?( s4 J# O( tExample: Fibonacci Sequence, T5 _4 ]) B3 f8 D

    ' V1 J9 @+ ^9 f+ k) }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ O3 j; T2 d9 {9 Y$ s. Q* n' A2 _
    7 c1 U- E+ g" W) h# W
        Base case: fib(0) = 0, fib(1) = 1
      P# B+ B- H; P1 Q% }+ L3 D
    9 A! M! K  ~5 w" }$ |    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    * W4 P& s3 a6 ^7 l7 |: [: U+ i8 r, g" p
    python6 D5 a) @3 Q+ N0 o! b) w! `1 G
    ! I$ X5 D1 \0 m
    , }& @( M% h  t9 W/ H/ l
    def fibonacci(n):
    ' ~7 q* Y& c! L+ }    # Base cases
    3 p( L4 T' J8 u    if n == 0:
    ) u% ?4 }1 ^% q7 k- m        return 0
    # _) R! P7 f$ U0 X5 w    elif n == 1:
    0 t: D8 L7 W8 v8 _1 H# }        return 1# a  {$ e$ s. ]# d
        # Recursive case' M4 v" n  g; n
        else:
    0 D) ]6 p  r$ E* R- [        return fibonacci(n - 1) + fibonacci(n - 2)
    ; Q6 A9 C1 `* C. ^8 n8 i
    6 i, X' r: {- F9 e# Example usage7 n3 Y9 _; S  o; e- i
    print(fibonacci(6))  # Output: 8
    0 }8 Q1 e; l  i& J7 v! r( ]- g$ t. b$ ~/ C* R: [% L
    Tail Recursion& u8 }. Q' P  N6 v2 r1 t# n9 r4 N, Y

    - `7 Z6 a( x$ f" A. L+ QTail 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).1 o$ p" l3 g' v$ M" _! }4 m

    0 i$ ]9 E# H- T: ~0 ~# }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-2-6 00:13 , Processed in 0.056355 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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