设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , H2 o- {9 k9 b( v  z7 i$ u, A/ h2 Z

    " O0 b0 q' H3 s7 W) F6 Y4 t解释的不错
    - U) q5 v, p! A* v* x- ?% R) w1 [
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " b6 {, _  R3 r: X$ ^: ?: f" Y. B6 b' w) z3 O  y# q* D$ G
    关键要素
    3 P5 k2 F  i2 A" B1. **基线条件(Base Case)**
    6 A) \7 [5 I0 f) P+ S  m! c" \! y   - 递归终止的条件,防止无限循环
    1 v8 ~! u  e' G1 c/ N- V   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    6 l5 O; _* F% o5 M. G0 s: B! t- o/ y5 R, K
    2. **递归条件(Recursive Case)**
    / Q7 Y3 [/ J; E; D( b   - 将原问题分解为更小的子问题! f5 R+ R( I. S5 f
       - 例如:n! = n × (n-1)!2 p# D# S" {3 o0 a5 U

    2 x5 b  @8 R& U. d 经典示例:计算阶乘
    8 {, B$ c( f' u0 M7 npython
    4 c& T( f4 k# z4 r" ?( I2 Pdef factorial(n):9 l" k: R% p, ^+ u) ?) b, B
        if n == 0:        # 基线条件
    " L; z9 P1 D! \( L        return 1
    2 O7 W6 e) s& H9 U' M    else:             # 递归条件
    ! ~- Z0 d& S* Y+ s) \        return n * factorial(n-1)( y! ~' C: K! r; h$ U+ `, K: f
    执行过程(以计算 3! 为例):
    , j- v# |0 H9 R$ }. Yfactorial(3)
    ! w6 [7 B+ n2 G- r8 X+ _$ a3 * factorial(2)
    " M) d' R( \5 f+ n3 * (2 * factorial(1))
    ! h% r& g1 p4 u6 L3 * (2 * (1 * factorial(0)))3 {# ^; F( q( g4 T) ^7 \
    3 * (2 * (1 * 1)) = 6! A. |, r( D. X: r" k7 X2 ]
    . ]+ p* @4 p0 F" w5 e* e
    递归思维要点1 s, ?9 F7 l% e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑- V( J3 i0 F& `5 X
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)) ^; J; y- A) J4 q' U4 T
    3. **递推过程**:不断向下分解问题(递)  z5 f+ {+ L* a, \. O0 |' E, M2 M
    4. **回溯过程**:组合子问题结果返回(归)
    - v* k: R, k! e8 z" M6 q6 B7 a( f0 |7 K  u; q4 h
    注意事项
    % ?8 l7 B9 E! `( m  }3 a! ?必须要有终止条件
      W6 K* i$ f  V6 G+ X4 Y/ E递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    / g/ U" }' k* ?某些问题用递归更直观(如树遍历),但效率可能不如迭代# h. q% [+ |9 Q* }% j: d
    尾递归优化可以提升效率(但Python不支持)
    ( R& v  ~1 a& U4 t- P" X
    * A# _  l) ~0 l5 z+ Q 递归 vs 迭代
    6 v2 l: y$ s# v+ [' v|          | 递归                          | 迭代               |# \; C7 _8 s( T! a
    |----------|-----------------------------|------------------|
    . U$ f+ @: L0 d+ s& t2 || 实现方式    | 函数自调用                        | 循环结构            |! b" s% w& I4 `4 X0 ]
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    - W5 S2 N6 h$ w| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      c! j$ X7 L# ]/ u2 v| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 w$ s- h" B- P: g2 A  J* r( B# y# A* V" ~# Z4 H& e; n. s
    经典递归应用场景
    / q  a+ q% d) h; F1. 文件系统遍历(目录树结构)
    & j, R0 s) X; P% o6 Y2. 快速排序/归并排序算法  }! ^/ b, b& S4 {5 b
    3. 汉诺塔问题
    ; h1 x8 w$ [/ A+ t6 K- C4. 二叉树遍历(前序/中序/后序)' [$ H, o  B! u" w
    5. 生成所有可能的组合(回溯算法)& P. ^' a1 H$ f6 Y
    # I8 h" p0 Z  ^8 ]+ A: l
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ V! K& q9 W1 b6 m+ }8 D8 [
    我推理机的核心算法应该是二叉树遍历的变种。
    . Q5 T4 g0 N8 U& b, D% v: 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:; d0 V  T3 d) Y3 @$ x% P) E
    Key Idea of Recursion
    2 v! _0 G  E( g; a! T1 G* c8 J+ C' u$ V7 ^3 i
    A recursive function solves a problem by:" H  @8 r' s0 @7 D0 g

    & b6 Q# M- M4 o    Breaking the problem into smaller instances of the same problem.- }( ]& ]% h7 i3 S  x

    : z- K$ d8 L( I. D  y0 z    Solving the smallest instance directly (base case).
    + J/ A: i) H$ j0 B6 K- Q
    5 c& ]- q0 \2 J) F3 l$ N& \0 w    Combining the results of smaller instances to solve the larger problem.' A, y' G: q" a' v% i2 v/ I* h
    ' G& t# F8 y5 b* R
    Components of a Recursive Function& Z; u& D  [3 q( ?- v9 M

    * S% j6 V0 c) U! `" F    Base Case:
    + l6 [6 L1 q% U7 t* _7 k1 c$ m! X+ i" D0 d6 t
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* E+ }* E7 n4 q4 X3 A* U

    $ ~6 S+ k6 F) ?* _, s        It acts as the stopping condition to prevent infinite recursion.
    ! G9 _. J3 t$ b' r9 r7 E( e' y9 J: w. S) F$ T
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * {0 X8 B% p* s; t
    ) _8 D: W" T* |9 U/ _! Y( D    Recursive Case:
      P: q+ n, c$ B! d: L8 U! d2 P7 j( J( N
            This is where the function calls itself with a smaller or simpler version of the problem.# s9 P3 p; G9 z2 y1 H
    # v* |  s/ v5 i" S  O
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).) b& \4 ^3 J" F9 Q  \

    6 O: V4 N1 @6 H/ h  G9 CExample: Factorial Calculation; u4 M; L9 \) r7 R0 Q

    , q! L) ~7 K: 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:& n. ^* ], f  Z

    7 h7 ^. z- u4 D5 Y: W* H; R    Base case: 0! = 18 N  ~) `6 c" Q0 G* k4 V0 k
    ' h8 W! O$ T$ s& Z) `
        Recursive case: n! = n * (n-1)!
    ; F5 h* e# D5 x2 A) u" [5 S% {! N: T; W  q
    Here’s how it looks in code (Python):
    . K  ~4 b1 v- T6 j( Npython
    2 E7 `1 N$ a, [. O' h
    9 ]+ W* e* N3 D2 A: K
    - ]. {5 s$ o5 n+ b) g# m' ydef factorial(n):
    % A1 ]4 m* ^, ~4 z( k( r$ J    # Base case
    : c, d! ?2 U7 u5 e: O    if n == 0:2 W) C+ v3 f6 z0 a# v# d
            return 1
    , C& V8 a0 m2 s$ h    # Recursive case
    4 L- j; Z( {( z; W5 B9 q$ X7 N+ i    else:
    3 D) w+ A" R; ~& k: V* ?* V        return n * factorial(n - 1)
    6 m4 `4 I1 a, d9 D0 a) }0 D
    7 \* \, H8 ^0 N8 u( ?: t$ D$ [# Example usage7 t0 z; z5 |& R( z
    print(factorial(5))  # Output: 120$ |7 G8 s1 g2 Q; I

    * X  x/ E* |& V! UHow Recursion Works; I; N) Y2 r$ [2 }7 b! F. G8 y6 P
    ' E( h) Q( G( N9 f0 y0 y( P
        The function keeps calling itself with smaller inputs until it reaches the base case.4 f/ l) u0 H* c2 F

    " E" n  `" }$ p& t9 \& Z: T    Once the base case is reached, the function starts returning values back up the call stack.
    7 Q# |  `0 x! n7 @1 ~
    4 a2 I$ h" Y  U1 j0 m9 d    These returned values are combined to produce the final result.
    - j  p' X' a6 Q, b" k8 I! J1 Y
    - `: w6 g5 `, N# T2 R, eFor factorial(5):" L* ?/ H8 l& Z- ?2 \. M
    . @$ t* q& U" v' ^5 _& a+ F& o
    % T  ~+ k, ?( S$ Z) ^
    factorial(5) = 5 * factorial(4)
    8 i# j# x, A. `: c- rfactorial(4) = 4 * factorial(3)
    ) h* g$ w2 z9 G- \/ c2 P( W9 w, Dfactorial(3) = 3 * factorial(2)8 p  u1 j, h% h/ I! d6 s, |2 _
    factorial(2) = 2 * factorial(1)
    8 w+ p4 v1 s+ H4 _factorial(1) = 1 * factorial(0)4 F( v$ K" F0 T  |" l& y
    factorial(0) = 1  # Base case
    / Q" I) n4 o# a* H
    , U. _1 k+ X& K: L/ B/ TThen, the results are combined:! B! }9 s$ b: c( c0 X* D

    . Q6 s: U5 Z& o# k6 E( G9 W* O5 B
    / }3 Z& I, Q4 [3 O0 ?- y7 A; Kfactorial(1) = 1 * 1 = 1
    4 j- ?' D* R& T$ rfactorial(2) = 2 * 1 = 2
    7 ]) f% X& Y( p/ s5 v5 Dfactorial(3) = 3 * 2 = 6" e6 ^+ K: |5 c
    factorial(4) = 4 * 6 = 245 v. `) Q' D& v; v. P
    factorial(5) = 5 * 24 = 120/ q% \7 M  L) A9 f) R- e6 c
    # [3 D& B( d5 U1 I
    Advantages of Recursion
    * Y. o% ~% n$ m3 m+ D# [+ Y1 G6 Q2 `+ V1 p7 P1 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).
    / W3 k3 v, Z3 g# s' N1 e3 [" j" r0 s$ \1 |7 N
        Readability: Recursive code can be more readable and concise compared to iterative solutions.- b. a- N( X/ U
    1 a. C( R; [; M. g$ _) [5 T
    Disadvantages of Recursion
    9 [0 K" G3 R/ \0 e- H# @* y  D0 W1 A8 u$ U7 w6 w; d2 Q" P5 r. n
        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.) }' T2 y: E  z5 b! e, q
    - B8 U1 N: p3 s6 Q/ U
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ; `$ y( N. I0 n( @* p2 m( n7 a% j9 T4 }# U* i1 ]
    When to Use Recursion
    . F0 O* R4 H, I( x- D- p6 \) e% A) I3 x- }0 D' i
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).: I+ b; P/ T3 y3 u
    1 v* \* [* p2 b  L4 S6 T; {' q
        Problems with a clear base case and recursive case.
      ?( ~7 w* T! S0 E& p9 i7 l, K" t7 ~6 Q8 g
    Example: Fibonacci Sequence7 M. R7 v2 L* ?, ~/ r& L

    9 c1 \1 f& C0 h. o. y4 f! |The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:$ \" \1 a) e7 h4 }
    ! m( t4 q& _' {7 W
        Base case: fib(0) = 0, fib(1) = 1
    " w' I4 Z9 M) F
    0 c, d) x$ g7 W    Recursive case: fib(n) = fib(n-1) + fib(n-2)$ `+ Q" |9 \, ^, a9 Y( y
      Z1 V7 r& Y+ X9 L/ t  y
    python
    $ U2 S+ m9 g, X) y- \+ _  O  c0 @, T/ [

    # D8 u4 x& K8 U, e5 ydef fibonacci(n):
    8 Q' \; C! I; C( o6 @7 ?    # Base cases
    4 ~0 E6 ^* \# D9 N0 x* a6 q: q    if n == 0:/ }# e1 v( M! Y: a& n7 b
            return 0; \9 x% \1 E: w0 U# R/ ^& b& I
        elif n == 1:: B$ C' o; r& R  ^: C1 V- c
            return 1, V/ c1 q9 {- H4 W* l' Q
        # Recursive case/ }6 ~, B2 x" Q* n9 D! g/ j
        else:
    ! i6 N  F/ U# s: ]: {        return fibonacci(n - 1) + fibonacci(n - 2)( a6 s5 U/ z0 k4 V
    : @# c1 r/ ~( m+ J3 S
    # Example usage
    + D* ~' r6 T3 K; e+ Eprint(fibonacci(6))  # Output: 88 V: U' k  j/ q. V1 U5 Z, x+ Q
    5 J/ ?4 ?  _& g
    Tail Recursion
    ( [/ U& j5 ]3 v+ c: |$ C' r
    ' G* `5 ^, O" o4 xTail 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)./ }3 y2 z+ W% C' S
    5 c! [) P4 ~, \$ }# W2 S5 G
    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-5-3 21:41 , Processed in 0.063936 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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