设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 P6 j. m! |2 u6 d, t+ D: m
    . k0 |, T8 r9 K( o6 J解释的不错
    & @7 A4 b) s! t1 s8 R+ h1 X2 ~3 P! E0 S+ ?
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! R" P4 F+ d) A8 W

    2 }6 f  B& h, q; \& x( {. `& ]' ? 关键要素9 I- M- a5 W! ^  H- g+ Z8 ]
    1. **基线条件(Base Case)**
    ' Y. q6 ^8 Q# o& u  v   - 递归终止的条件,防止无限循环& h, p& q2 f) Y4 @9 u- _+ l
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 k: i$ u( Q' l+ A$ ~
    " o- h+ ]6 x( u& V( g- e% W
    2. **递归条件(Recursive Case)**
    4 }3 t2 f& _. X: M   - 将原问题分解为更小的子问题
    ) D% M+ K! a' d  h: Q   - 例如:n! = n × (n-1)!
    4 ^( y8 N) l9 W9 y: O% X- J: L" b$ _) i; |3 H7 U
    经典示例:计算阶乘; z7 K3 o" p/ W
    python# i" j* H; o) l, l. }; \* z
    def factorial(n):0 n- s# e2 O& u) R6 S" e( m6 ?" O
        if n == 0:        # 基线条件( B! E4 S/ F& N& ~" X6 I: |& `
            return 1- _! {# ]" e+ A  v
        else:             # 递归条件
    ! B- A9 r. J3 k$ K        return n * factorial(n-1)
    , q; e' |" ?; a6 q执行过程(以计算 3! 为例):
    8 b, P' P2 @( G9 Afactorial(3)
    5 B# ?) M8 o9 M8 L3 * factorial(2)% m6 T0 e( b4 F6 z; r1 {, `
    3 * (2 * factorial(1))5 u/ \$ J; T( h$ @$ K
    3 * (2 * (1 * factorial(0)))
    4 o& o( A1 H3 s) t$ K- ]  A5 C3 * (2 * (1 * 1)) = 69 h* ^! U& _$ q, x4 K) \

    3 j$ C0 u0 o$ C' L4 N9 [/ Z 递归思维要点* t/ M6 |+ r" a( g' @% ^( e) g
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    + Q" {. B: \8 d, l) N2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ) O% q6 g. e7 t3 W$ k! b4 C3. **递推过程**:不断向下分解问题(递)
    ' F8 D0 {6 d) T; m1 s; h: e4. **回溯过程**:组合子问题结果返回(归)$ @5 A' N: I1 p- H4 `

    7 G# [0 w( \; P! c3 W, ]% k 注意事项8 F) t) |) r, c) U5 N! a
    必须要有终止条件
    % e9 s. R5 Q* G* u5 [5 h4 P3 c' J0 W! |递归深度过大可能导致栈溢出(Python默认递归深度约1000层)$ h! g" R& a# L+ l; h& g
    某些问题用递归更直观(如树遍历),但效率可能不如迭代2 g8 k# g0 b! R! j( j
    尾递归优化可以提升效率(但Python不支持)
    5 g' @' c  n& C  j1 N0 ]( v4 T  _9 k6 d- {( S; X' w( s7 ]- f( q
    递归 vs 迭代5 n, K6 d! B% i
    |          | 递归                          | 迭代               |
      T3 v2 Z4 Y. D, V" G|----------|-----------------------------|------------------|* o( _, j- Y8 ~0 T, i0 Y  _
    | 实现方式    | 函数自调用                        | 循环结构            |+ E0 `  c3 L  |! T, G' [
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |/ @4 b4 g5 ~' c5 ~
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |+ s0 Q) ?" M" Z. w
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |3 e7 E# T# S& W& z3 L
    ) @: K1 i' B4 o
    经典递归应用场景
    ! T! H6 E* P2 o( y; h! w, y2 N' A1. 文件系统遍历(目录树结构)' b; A$ k" \9 }$ q
    2. 快速排序/归并排序算法
    + R8 Z4 h' ~4 K" `3. 汉诺塔问题
    - V. v4 h, p- x% }4. 二叉树遍历(前序/中序/后序)/ Z9 ?# }- [/ D: `5 b) V  g
    5. 生成所有可能的组合(回溯算法)2 w: E8 n- z" c+ J6 U8 n% y; @" d
      k! ^! |0 J# I5 S, T
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ k( q& ~% g4 @* K$ n
    我推理机的核心算法应该是二叉树遍历的变种。
    0 B+ h; g" U7 x/ x3 {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:7 l# y$ G; T6 j8 J2 b  K
    Key Idea of Recursion
    1 ^4 ]# z% R% B% Y
    % N( {) A2 @5 K  w+ Q3 |A recursive function solves a problem by:; o2 G0 Z" [+ |, I! Q

    2 @6 ?  j( ]% f$ I0 ]0 X    Breaking the problem into smaller instances of the same problem.
    2 u6 X* R) ?3 P% L5 Z7 h: f* d6 c# p/ ?  l
        Solving the smallest instance directly (base case).
    2 n7 \6 W4 ^: r" T) n9 a* l! y% T* i  K
        Combining the results of smaller instances to solve the larger problem.1 I0 H+ t( c! C

    ( B% I& C& k+ o+ s  wComponents of a Recursive Function
    7 A, j7 @$ V/ ~+ j( k' i6 t9 G) P- {
        Base Case:% V5 B9 b% I) o5 b- z
    3 C4 B0 M; H: M9 N0 a; N6 X
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( l& A9 k6 z3 c5 P  l8 o, V" R0 B" o# c4 f& k
            It acts as the stopping condition to prevent infinite recursion.9 I1 B+ v, T+ H/ |
    4 i# _( q& R! W6 Q5 v
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    7 {# Z3 R6 s- P) H! @& f6 J+ p
    - H& t6 R! H+ x9 V0 B    Recursive Case:% G1 l9 }" [& Z1 P: Y, g
    8 N( [# y! v9 Z8 [
            This is where the function calls itself with a smaller or simpler version of the problem." L) ^# d' M8 U, ~, h6 z5 v& N$ Z

    ; I! A& T4 P: @4 }        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 B) x/ b$ l% P( f5 x6 S( X' _) u$ v1 Y: D1 e0 s
    Example: Factorial Calculation
    1 L+ d& d. H" X' L1 n( z+ a4 g0 r2 o7 P
    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:2 m9 t- C. H' r5 L. o

    6 L+ a0 w  V3 s5 B9 m: L: Q8 I' H    Base case: 0! = 1
    , q6 X9 Q. M% {! z, U7 t2 |$ V7 L. ?- Z! `
        Recursive case: n! = n * (n-1)!
    3 s1 o; A- K+ O/ |
    1 N  _% r! i1 Y  d- eHere’s how it looks in code (Python):
    1 b- @2 g! b$ V; V) P8 Kpython* f3 l6 A' F$ W/ r7 D
    * N+ G0 l2 a* Z4 O

    1 I* s; _' N- Ldef factorial(n):: O. I  M" t2 t. g3 P
        # Base case
    8 r7 a+ M4 `# e+ P4 L5 c$ P, N* i    if n == 0:
    ' i3 x. x' C: t8 `; r7 }        return 11 m  T- W( q1 o0 ~& j
        # Recursive case
    ! ~6 J- A7 [3 I) @% u; P    else:) _2 Q3 n0 h: i) Q; Z% ]. K# T
            return n * factorial(n - 1)
    ; i, b7 i: y- d0 f* Z5 U* ~4 P0 E' R. {
    # Example usage; q' s' Y8 S( n- }" d8 w9 i
    print(factorial(5))  # Output: 120
    ) T4 h4 I+ y% m$ h9 i4 K$ m: l4 H/ s: `" _+ L# i6 Y; |7 }. {9 s; o
    How Recursion Works
      Z- i5 b! f3 P7 t5 g
      {$ k. Z. q9 U0 n9 E3 w* b! B0 s; r    The function keeps calling itself with smaller inputs until it reaches the base case.( \# K% B$ d7 ^; _
    - m1 j( |: B5 D0 b
        Once the base case is reached, the function starts returning values back up the call stack.# e- A, T* R5 X0 x6 x- ~+ g
    1 G; w  I( }. D) p0 {
        These returned values are combined to produce the final result.
    1 ]. u, w: S4 M& @% Q
    9 T0 \5 l9 ^. }: Q  ~7 KFor factorial(5):
    ( i. x2 x  O. d5 b0 t/ J" v1 A+ @$ M4 P
    7 q& Z1 q) v, h5 n; [" K
    factorial(5) = 5 * factorial(4), S% `$ I& _& P2 L0 |
    factorial(4) = 4 * factorial(3)
    4 z9 R# [% B0 i. sfactorial(3) = 3 * factorial(2)
      \4 J: \0 B- _' jfactorial(2) = 2 * factorial(1), Z7 j( Z0 a- `" ~6 W' k
    factorial(1) = 1 * factorial(0)
    ' S) K3 ^1 v2 Vfactorial(0) = 1  # Base case& r/ c1 r6 N. V4 x) T

    2 W# d8 m9 R3 k; F. o" N, yThen, the results are combined:: r* D$ s0 J7 g; f/ @+ @& T
    7 {. a' B7 y$ F5 E

    4 N* G' `# J( ffactorial(1) = 1 * 1 = 1
    ! E# n" f- W, x, \% X* wfactorial(2) = 2 * 1 = 2
    . @$ g8 m) [) N) W) Efactorial(3) = 3 * 2 = 6
    ; Z0 C7 D' T1 |4 b5 C0 q' V+ ?6 Efactorial(4) = 4 * 6 = 24
    3 a  \5 W. b; e4 v5 i2 `1 a9 u. sfactorial(5) = 5 * 24 = 120
    9 d( Y+ J& A  C+ }; j
    : b& F4 z0 W% k7 k; nAdvantages of Recursion* {, B, m$ `% r

    " K' ~( z. V% Y) Z+ V7 [    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).
      V* d; e  Y! k
    ; a6 A% `+ u, s* g    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " u; ]& y6 I9 H* L
    0 h4 o+ `: m9 z2 X$ H) C) n' jDisadvantages of Recursion
    6 h5 Z# f7 k% G; Z3 r: j% S; b0 L6 ?7 G
        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.5 c( u6 Y6 T6 e0 t
    * k5 J8 L, F1 L1 E' }
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)." h7 V" j) a' e2 o3 f8 i  w8 Z
    , Z6 H* C2 M; I2 b
    When to Use Recursion1 U% L! D7 F& Y
    " m7 J' b  c5 w0 X# v5 E
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + U2 o( O, ^/ L9 Q; a
    2 j2 h; U) j0 X8 K    Problems with a clear base case and recursive case.
      _6 x( z# _* z  k5 R1 I
    * ^6 B+ n# Q8 z5 ZExample: Fibonacci Sequence$ |3 C- ~( J' m; ^
    & ~. Y! y/ ?5 D  ^$ W7 _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& [6 U* G% X5 r/ k, V! E2 `7 K
    8 G9 J9 Q4 f1 Z) A. s9 R
        Base case: fib(0) = 0, fib(1) = 1
    * Q; e7 O4 ~; A( d# ^" r
    $ P" ^+ y7 t0 G' [; B/ |    Recursive case: fib(n) = fib(n-1) + fib(n-2)  L; |2 k: F! V" U+ S- x8 S0 s

      }$ K. M  _- N# m7 Rpython6 T/ m' q8 k' M; w
    7 k3 h* p; J+ ~) R) N( t/ P
    1 v' b3 d1 ^: O
    def fibonacci(n):0 ^: {) ]* l6 I6 O. n! @
        # Base cases
    & \! q; l" I% I8 b    if n == 0:
    4 y; v9 g1 Y3 x+ u2 a6 x        return 0
    # i8 Y. y5 B; t/ O) n4 T0 C    elif n == 1:; o2 K( ~: `  ]# Z! V9 ]
            return 1% t& h* D( O$ W7 d4 \# T
        # Recursive case
    9 _0 M+ d: O* e/ g+ _: j% n1 Q    else:* ^  j6 Y- k8 h, O
            return fibonacci(n - 1) + fibonacci(n - 2)
    & _5 o) b" Z$ a. Z" \
    0 f) m, M: K6 `7 e* i, D# Example usage/ o2 ?  n, x& B: S* n
    print(fibonacci(6))  # Output: 8' e5 }( A; C! [7 |
    ! F3 e2 @. u' o3 H) n
    Tail Recursion
    " A5 F) ]  D, t% f6 G! {
    ( M/ S0 q) o! g0 e8 k5 d  eTail 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).! X4 i- K3 @  J$ U
    ! B3 T; I+ c% X" {
    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-20 13:02 , Processed in 0.058854 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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